Datagridview删除重复的行

时间:2014-03-13 07:35:29

标签: c# datagridview

我需要你帮助删除datagrid中的重复项。我做了这个,

  foreach (DataGridViewRow viewrow1 in this.dataGridView1.Rows) 
        {

            DataRowView row1 = viewrow1.DataBoundItem as DataRowView;
            if(row1 != null){
                foreach (DataGridViewRow viewRow2 in this.dataGridView2.Rows)
                {
                    DataRowView row2 = viewRow2.DataBoundItem as DataRowView;
                    if (row2 != null && row1.Row["test"].Equals(row2.Row["test"]))
                    {
                        row2.Row.Delete();

                    }

                } 
            }

        }

它有效,但不会删除1行。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

  foreach (DataGridViewRow viewrow1 in this.dataGridView1.Rows) 
    {

        DataRowView row1 = viewrow1.DataBoundItem as DataRowView;
        if(row1 != null){
            foreach (DataGridViewRow viewRow2 in this.dataGridView2.Rows)
            {
                DataRowView row2 = viewRow2.DataBoundItem as DataRowView;
                if (row2 != null && row1.Row["test"].Equals(row2.Row["test"]))
                {
                    dataGridView1.Rows.Remove(row2);

                }

            } 
        }

    }