简体   繁体   English

显示 DataGridView 的已删除行的索引

[英]Show the index of a deleted row of a DataGridView

I want to show in a second column of my datagridview the index of the deleted rows.我想在我的 datagridview 的第二列中显示已删除行的索引。

private void btn_Verify_Click(object sender, EventArgs e)
        {
            var dataA = new HashSet<string>();
            var dataB = new HashSet<string>();
            var exceptA = dataA.Except(dataB);
            var exceptB = dataB.Except(dataA);
            for (int i = 1; i < dgv_A.Rows.Count; i++)
            {
                dataA.Add(dgv_A[8, i].Value.ToString());
            }
            for (int i = 1; i < dgv_B.Rows.Count; i++)
            {
                dataB.Add(dgv_B[8, i].Value.ToString());
            }
            if (dataA.SetEquals(dataB))
            {
                lbl_result.Text = "Certifications are correct";
            }
            else
            {
                lbl_result.Text = "Certifications are incorrect";

//A function is created as the DataGridView expects the object to generate the column.
                dgv_B.DataSource = exceptB.Select(s => new { Value = s }).ToList();
                dgv_A.DataSource = exceptA.Select(s => new { Value = s }).ToList();

                dgv_B.Columns[0].HeaderText = "Deleted Files from First Archive";
                dgv_A.Columns[0].HeaderText = "Deleted Files from Second Archive";
            }
        }

This is the result if the HashSets are unequal, I want to add a second column in each DataGridView with the index of the row removed, all ideas are welcome.这是如果 HashSet 不相等的结果,我想在每个 DataGridView 中添加第二列,并删除行的索引,欢迎所有想法。

结果

Im assuming the index you want to show is from where the item sat in the original list.我假设您要显示的index来自该项目在原始列表中的位置。

You can achieve this with minimal change by using a HashSet<(string, int)> to store the information and then using an annonymous class with two fields in the select您可以通过使用HashSet<(string, int)>来存储信息,然后在annonymous中使用具有两个字段的匿名select来实现此目的

private void btn_Verify_Click(object sender, EventArgs e)
{
    var dataA = new HashSet<(string, int)>();
    var dataB = new HashSet<(string, int)>();
    var exceptA = dataA.Except(dataB);
    var exceptB = dataB.Except(dataA);
    for (int i = 1; i < dgv_A.Rows.Count; i++)
    {
        dataA.Add((dgv_A[8, i].Value.ToString(), i));
    }
    for (int i = 1; i < dgv_B.Rows.Count; i++)
    {
        dataB.Add((dgv_B[8, i].Value.ToString(), i));
    }
    if (dataA.SetEquals(dataB))
    {
        label1.Text = "Certifications are correct";
    }
    else
    {
        label1.Text = "Certifications are incorrect";

        //A function is created as the DataGridView expects the object to generate the column.
        dgv_B.DataSource = exceptB.Select(s => new { s.Item1, s.Item2 }).ToList();
        dgv_A.DataSource = exceptA.Select(s => new { s.Item1, s.Item2 }).ToList();

        dgv_B.Columns[0].HeaderText = "Deleted Files from First Archive";
        dgv_A.Columns[0].HeaderText = "Deleted Files from Second Archive";
    }
}

This being said I think you have a bug in your code to because your loops iterating over datagrids start at index 1 so you can never add the first item even if it is deleted - but thats not related to your question.话虽这么说,我认为您的代码中有一个错误,因为您的循环遍历数据网格从索引 1 开始,因此即使第一项被删除,您也永远无法添加第一项 - 但这与您的问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM