Jan 8, 2010

Eliminate Duplicate records from the Grid View

Most of the time we need to delete duplicate entries from GridView, in this case we can delete it manually also or using onRowcreate also we can remove that .

Example
suppose Mr.Xyz has has more than 2 Account in Blogspot.com site and we want to show
in GridView Person and RelatedBlogs like this





and in this case we dont want to repeat Person name on each row.
- I have a data source

DataTable dtsource = new DataTable();

//Adding columns to datatable
dtsource.Columns.Add("Person");
dtsource.Columns.Add("Related Blog");

//Adding records to the datatable
dtsource.Rows.Add("kiran", "kirank.blog.com");
dtsource.Rows.Add("kiran", "webdevlopmenthelp");
dtsource.Rows.Add("kiran", "www.kiran.com");
dtsource.Rows.Add("mark", "mark.blog.com");
dtsource.Rows.Add("mark", "asp-net.blogspot.com");


put the below code in codebehind


private void GenerateUniqueData(int cellno)
{
//Logic for unique names

//Step 1:

string initialnamevalue = grdUniqueNames.Rows[0].Cells[cellno].Text;

//Step 2:

for (int i = 1; i < grdUniqueNames.Rows.Count; i++)
{

if (grdUniqueNames.Rows[i].Cells[cellno].Text == initialnamevalue)
grdUniqueNames.Rows[i].Cells[cellno].Text = string.Empty;
else
initialnamevalue = grdUniqueNames.Rows[i].Cells[cellno].Text;
}
}



on GridView define property OnRowCreated="GenerateUniqueData" it will search the record via cell and set the Person name as a empty or person name

No comments:

Post a Comment