1

this is my datagriddatagrid with duplicate value

i tried the following code for removing the duplicate row in datagridview

public static DataTable items = new DataTable();
items.Columns.Add("Backsn");
items.Columns.Add("Oprn Name");
for (int i = 0; i < dataGridView1.Rows.Count;i++ )
  {
 DataRow rw = items.NewRow();
 rw[0] = dataGridView1.Rows[i].Cells[2].Value.ToString();
 rw[1] = dataGridView1.Rows[i].Cells[8].Value.ToString();
 items.Rows.Add(rw);
}
dataGridView2.DataSource = items;
for (int i = 0; i < dataGridView2.Rows.Count; i++)
        {
            int k = 0;
            for (int j = 0; j < dataGridView2.Rows.Count; j++)
            {

                if (dataGridView2.Rows[i].Cells[0].Value == dataGridView2.Rows[j].Cells[0].Value && dataGridView2.Rows[i].Cells[1].Value == dataGridView2.Rows[j].Cells[1].Value)
                {
                     if (k != 0)
                    {
                        items.Rows.RemoveAt(j);
                        dataGridView2.DataSource = items;
                    }
                   k= k+1;
                }
            }
        }

But no luck. I should get result like below.Please help me to solve.

without duplicate

Archana Palani
  • 247
  • 1
  • 6
  • 23

4 Answers4

2

In case, it is bounded with the DataTable, you don't need to make changes in the datagridview, in spite of, you should apply removing the duplicate rows in the dataTable itlsef. and here's you can try one way-

DataTable items = new DataTable();
items.Columns.Add("Backsn");
items.Columns.Add("Oprn Name");
for (int i = 0; i < dataGridView1.Rows.Count;i++ )
{
   DataRow rw = items.NewRow();
   rw[0] = dataGridView1.Rows[i].Cells[2].Value.ToString();
   rw[1] = dataGridView1.Rows[i].Cells[8].Value.ToString();
   if (!items.Rows.Cast<DataRow>().Any(row => row["Backsn"].Equals(rw["Backsn"]) && row["Oprn Name"].Equals(rw["Oprn Name"])))
       items.Rows.Add(rw);
}
Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
  • your code is working perfect !!!! but the thing is cell value FL85033000EG5YYA5 is not equalling while executing code ,its wired ,do i need to covert or something to compare the cell value? – Archana Palani Feb 05 '15 at 05:54
  • @ArchanaPalani, That might be possible because you are using '==' equals operator that by default checks for the reference except immutable types. for value equality checks, you must be sure to check with object.Equals(object) method – Rohit Prakash Feb 05 '15 at 05:57
  • Thank you so much Rohit , you solved my problem :-) thanks a lot :-) – Archana Palani Feb 05 '15 at 06:01
0

Replace if your condition with below code

 if (dataGridView2.Rows[i].Cells["Backsn"].Value == dataGridView2.Rows[j].Cells["Backsn"].Value && dataGridView2.Rows[i].Cells["Opm Name"]].Value == dataGridView2.Rows[j].Cells["Opm Name"].Value)`
                    {
Sheeba
  • 25
  • 2
  • 18
0

If your DataGrid is displaying data bound to any kind of DataSource, you ought to try to eliminate the duplicates at the datasource before the data is even bound.

For example, if your items is a collection of any kind of class T, you can have the class implement IEquatable<T> and redefine your DataSource as the IEnumerable<T>.Distinct() values from the collection, or convert your collections source to a HashSet<T>.

If you are bound to some sort of database query result or table, you can convert the binding to a view which eliminates duplicate rows.

mdisibio
  • 3,148
  • 31
  • 47
  • If you are manually creating the DataTable, then Rohit's code fits your needs for now. The real question is, what is the source of your data? A database query? A collection? – mdisibio Feb 05 '15 at 05:46
  • its collection , Rohit's code works perfectly , the same concept i have applied in my code , cell data's looks equal but when i execute in code which is not accepting the condition. – Archana Palani Feb 05 '15 at 05:52
  • @ArchanaPalani, That might be possible because you are using '==' equals operator that by default checks for the reference except immutable types. for value equality checks, you must be sure to check with object.Equals(object) method. – Rohit Prakash Feb 05 '15 at 05:55
0
   int RowSpan = 2;

        for (int i = grdGroup.Rows.Count - 2; i >= 0; i--)
        {
            GridViewRow currRow = grdGroup.Rows[i];
            GridViewRow prevRow = grdGroup.Rows[i + 1];
            if (currRow.Cells[0].Text == prevRow.Cells[0].Text)
            {
                currRow.Cells[0].RowSpan = RowSpan;
                prevRow.Cells[0].Visible = false;
                RowSpan += 1;
            }

            else
            {
                RowSpan = 2;
            }


        }

for another column in same grid

for (int i = grdGroup.Rows.Count - 2; i >= 0; i--)
            {
                GridViewRow currRow = grdGroup.Rows[i];
                GridViewRow prevRow = grdGroup.Rows[i + 1];

                if ((currRow.Cells[2].Text == prevRow.Cells[2].Text) && (currRow.Cells[0].Text == prevRow.Cells[0].Text))
                {

                    currRow.Cells[2].RowSpan = RowSpan;
                    prevRow.Cells[2].Visible = false;
                    RowSpan += 1;
                }

                else
                {
                    RowSpan = 2;
                }


            }
Code
  • 679
  • 5
  • 9