Skip to main content
 首页 » 编程设计

C# 在 datagridview 中搜索重复项

2024年02月23日15shihaiming

使用 ms visual studio 和 csharp .net4。

这是我必须检查重复的代码

    public void CheckForDuplicate() 
    { 
        DataGridViewRowCollection coll = ParetoGrid.Rows; 
        DataGridViewRowCollection colls = ParetoGrid.Rows; 
        List<string> listParts = new List<string>(); 
        int count = 0; 
        foreach (DataGridViewRow item in coll)//379 
        { 
            foreach (DataGridViewRow items in colls)//143641 
            { 
                if (items.Cells[5].Value == item.Cells[5].Value)   
                { 
                    if (items.Cells[2].Value != item.Cells[2].Value) 
                    { 
                        listParts.Add(items.Cells["Keycode"].Value.ToString()); 
                        count++; 
                        dupi = true; 
 
                        //txtDupe.Text = items.Cells["Keycode"].Value.ToString(); 
                        //this.Refresh(); 
                    } 
                } 
            } 
        } 
        MyErrorGrid.DataSource = listParts; 
    } 

这是在允许用户保存之前的检查。

private void butSave_Click(object sender, EventArgs e) 
    { 
        CheckForDuplicate(); 
        if (dupi == true) 
        { 
            txtDupe.Clear(); 
 
            dupi = false; 
        } 
        else 
        { 
            SaveMyWorkI(); 
            dupi = false; 
        } 
    } 

这是它正在查看的数据:

现在,我知道逻辑一定是有缺陷的,因为它无论如何都会保存。 我基本上是在 pareto1 上搜索每个单元格,看看用户是否做了任何重复,如果是这样,它不会保存,而是在另一个 datagridview 中显示部件号等....嗯,这就是计划。

所以有人可以看看这个然后告诉我

1) 这在我的逻辑 中哪里失败,如果检查正确又如何呢?

2) 列表是否可以添加信息,如果是这样,一个简单的绑定(bind)到数据网格 View 就足以显示结果吗?

3) 如果这只是一种非常糟糕的搜索方式,有人可以提供反射(reflect)我正在努力实现的目标的代码。

非常感谢您以后的评论。

更新:好的,感谢您的帮助,我的算法现在可以工作了,但我的最后一个问题是显示在 pareto 列上重复的零件号,而不是显示长度。

public void CheckForDuplicate() 
    { 
        DataGridViewRowCollection coll = ParetoGrid.Rows; 
        DataGridViewRowCollection colls = ParetoGrid.Rows; 
        List<string> listParts = new List<string>(); 
        int count = 0; 
        foreach (DataGridViewRow item in coll)//379 
        {  
            foreach (DataGridViewRow items in colls)//143641 
            { 
                count++; 
                if ((items.Cells[5].Value != null)) 
                { 
                    if ((items.Cells[5].Value != null) && (items.Cells[5].Value.Equals(item.Cells[5].Value))) 
                    { 
                        if ((items.Cells[2].Value != null) && !(items.Cells[2].Value.Equals(item.Cells[2].Value))) 
                        { 
                            listParts.Add(items.Cells["Keycode"].Value.ToString()); 
 
                            dupi = true; 
                        } 
                    } 
                } 
            } 
        } 
        MyErrorGrid.DataSource = listParts; 
        var message = string.Join(Environment.NewLine, listParts); 
        //MyErrorGrid.DataSource = message; 
        MessageBox.Show(message); 
 
    } 

即使消息框正确显示结果?绑定(bind)到我的数据网格时我是否遗漏了什么?

请您参考如下方法:

这是一个简单的示例,展示了如何在数据输入期间执行验证。您可以通过多种方式自定义错误的显示方式(包括某种自定义对话框来解决错误),这可能会为您提供更好的解决方案。

public partial class Form1 : Form 
{ 
    BindingSource bs; 
    DataTable dt;    public Form1() 
    { 
        InitializeComponent(); 
 
        BindingList<BindingClass> data = new BindingList<BindingClass> 
            {  
                new BindingClass{ Name = "one" },  
                new BindingClass { Name = "two"}  
            }; 
 
        dataGridView1.DataSource = data; 
        dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating); 
 
    } 
 
    void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
        foreach (DataGridViewRow row in dataGridView1.Rows) 
        { 
            if (row.Index != e.RowIndex & !row.IsNewRow) 
            { 
                if (row.Cells[0].Value.ToString() == e.FormattedValue.ToString()) 
                { 
                    dataGridView1.Rows[e.RowIndex].ErrorText = 
                        "Duplicate value not allowed";                     
 
                    e.Cancel = true; 
                    return; 
                } 
            } 
        } 
        dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; 
    } 
 
}  
 
    public class BindingClass 
    { 
        public string Name { get; set; } 
    } 
 
} 

当然,这并不总是符合您对用户喜欢使用什么的要求,但我认为看到另一个选项可能会有所帮助。