I would like to share a brilliant piece of code as a continuation to my Nuts N Bolts which I encounter on my day to day developments and maintenance here and there:
This is for removing repeated data from DataTale:
public DataTable FilterDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
foreach (DataRow drow in dTable.Rows)
{
if (hTable.Contains(drow[colName]))
duplicateList.Add(drow);
else
hTable.Add(drow[colName], string.Empty);
}
foreach (DataRow dRow in duplicateList)
dTable.Rows.Remove(dRow);
return dTable;
}
Using the function:
private void BindGrid()
{
try
{
DataSet ds = (DataSet)ViewState["ds"];
DataTable dt = (DataTable)ViewState["dt"];
DataTable dtFiltered = new DataTable();
dtFiltered = FilterDuplicateRows(dt, "software");
gvRolePackage.DataSource = dtFiltered;
gvRolePackage.DataBind();
}
}