You must Sign In to post a response.
  • Category: ASP.NET

    Avoid Duplicate Records in GridView Dynamically

    I Develop the ASP.NET Web Application it is used for the Textile Management System i used input fields in the GridView. the GridView Row are added by Dynamically on a button clickevent
    here i have same repeated Records in the GridView. How to avoid the Duplicate Records in the GridView?
  • #768365
    Here i attach the sample screenshot.

    Delete Attachment

  • #768367
    Hi,
    Add RowDataBound event to you gridview and try following code:

    protected void gvDemo_RowDataBound(object sender, EventArgs e)
    {
    string szOldVal = "";
    string szNewVal = "";
    for (int iRowCount = 0; iRowCount < gvList.Rows.Count; iRowCount++)
    {
    szOldVal = gvList.Rows[count].Cells[iRowCount].Text.Trim();
    if (szOldVal == szNewVal)
    {
    gvDemo.Rows[count].Visible = false;
    }
    szNewVal = szOldVal;
    }
    }

  • #768372
    HI,

    Create a datatable with below line of code and this datatable will hold all the distinct rows. Then we can bind our Gridview with this table.

    DataTable uniqueRowTable = FirstTable.DefaultView.ToTable( /*distinct*/ true);

    Debasmit Samal

  • #768404
    Try this "ToTable" method

    DataTable dt = new DataTable();
    dt.Columns.Add("Data");
    dt.Rows.Add("1");
    dt.Rows.Add("2");
    dt.Rows.Add("3");
    dt.Rows.Add("3");
    dt.Rows.Add("4");

    ViewState["dt"] = dt;

    DataTable dtPr = (DataTable)ViewState["dt"];
    dtPr = dt.DefaultView.ToTable(true, "Data");
    dgvData.DataSource = dtPr;
    dgvData.DataBind();

    Software Developer
    iFour Technolab Pvt. Ltd.

  • #768457
    Hi,

    As I seen you are adding records through application i.e. you are storing data into some temporary table like viewstate, so when add new record first check the condition whether table contains the same record or nor we have several options to do that, like DataRow[] collection and get selected items if count is greater than 1 then skip insert else insert etc...

    Ex:

    DataRow[] col=dt.Select("your condition");
    if(col.Count == 0)
    {
    //insert record into temp table and store the same in viewstate again
    }

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #768464
    use RowDataBound event

    string oldValue = string.Empty;
    string newValue = string.Empty;
    for (int j = 0; j < 2; j++)
    {
    for (int count = 0; count < gvList.Rows.Count; count++)
    {
    oldValue = gvList.Rows[count].Cells[j].Text;
    if (oldValue == newValue)
    {
    gvList.Rows[count].Cells[j].Text = string.Empty;
    }
    newValue = oldValue;
    }
    }


    or you can Remove (Delete) Duplicate Rows (Records) from DataTable using DataView
    see below snippet

    protected void RemoveDuplicates(object sender, EventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];
    dt = dt.DefaultView.ToTable(true, "Id", "Name", "Country");
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768503
    I am just creating it inside a Datatable and then DataTable is fetched from a ViewState date and then the same DataTable will be populated using the DataTable DefaultView ToTable

    DataTable dtSpider = (DataTable)ViewState["SpiderViewSate "];
    dtSpider = dt.dtSpider .ToTable(true, "Id", "Name", "State");
    GridView1.DataSource = dtSpider ;
    GridView1.DataBind();

    ToTable method takes the first parameter is of boolean type which is true means it will return distinct rows and the next parameters are the name of the Columns whose data you want to be distinct.

    Debasmit Samal


  • Sign In to post your comments