如何从c#中的datagridview中删除重复的行?

时间:2015-02-05 04:43:11

标签: c# datagridview

这是我的datagrid datagrid with duplicate value

我尝试使用以下代码删除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;
                }
            }
        }

但没有运气。我应该得到如下的结果。请帮我解决。

without duplicate

4 个答案:

答案 0 :(得分:1)

如果它与DataTable绑定,您不需要在datagridview中进行更改,尽管如此,您应该应用删除dataTable itlsef中的重复行。你可以尝试一种方法 -

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);
}

答案 1 :(得分:0)

如果你的条件用以下代码替换

 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)`
                    {

答案 2 :(得分:0)

如果您的DataGrid显示绑定到任何类型的DataSource的数据,您应该尝试在数据被绑定之前消除数据源中的重复项。

例如,如果您的items是任何类T的集合,则可以让类实现IEquatable<T>并将您的DataSource重新定义为IEnumerable<T>.Distinct()值从集合中,或将您的集合来源转换为HashSet<T>

如果您绑定某种数据库查询结果或表,则可以将绑定转换为消除重复行的视图。

答案 3 :(得分: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;
                }


            }