Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DATGRIDVIEW中计算行,并将重复行视为单个行?_C#_Winforms_Datagridview - Fatal编程技术网

C# 如何在DATGRIDVIEW中计算行,并将重复行视为单个行?

C# 如何在DATGRIDVIEW中计算行,并将重复行视为单个行?,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个datagridview,它包含许多行,其中一些行有重复的值 此datagridview是可添加的。。我经常添加和删除行 我想在每次添加行或删除它时计算行,并将重复行视为单个行。 我尝试了以下代码: int total = 0; for (int currentRow = 0; currentRow < dataGrid_Target.Rows.Count - 1; currentRow++) { DataGridViewRo

我有一个datagridview,它包含许多行,其中一些行有重复的值

此datagridview是可添加的。。我经常添加和删除行

<>我想在每次添加行或删除它时计算行,并将重复行视为单个行。

我尝试了以下代码:

int total = 0;
        for (int currentRow = 0; currentRow < dataGrid_Target.Rows.Count - 1; currentRow++)
        {
            DataGridViewRow rowToCompare = dataGrid_Target.Rows[currentRow];

            for (int otherRow = currentRow + 1; otherRow < dataGrid_Target.Rows.Count; otherRow++)
            {
                DataGridViewRow row = dataGrid_Target.Rows[otherRow];
                if (!row.IsNewRow)
                {
                    if (!Convert.ToString(rowToCompare.Cells[0].Value).Equals(Convert.ToString(row.Cells[0].Value)))
                    {
                        total++;

                    }
                    else
                    {
                        total = total+1;
                    }
                }
            }
        }

但这没有帮助。

在这种情况下,Linq是你的朋友:

return dataGrid_Target.Rows
    .Cast<DataGridViewRow>()
    .Select(r => Convert.ToString(r.Cells[0].Value))
    .Distinct()
    .Count();
我将逐一解释:

强制转换:不能在DataGridViewRowCollectionRows上使用linq,因为它没有实现IEnumerable,所以必须像这样强制转换它

选择:以字符串形式返回每行的第一个单元格

Distinct:仅返回不同的条目,即删除重复项


计数:统计不同条目的数量。

在这种情况下,Linq是您的朋友:

return dataGrid_Target.Rows
    .Cast<DataGridViewRow>()
    .Select(r => Convert.ToString(r.Cells[0].Value))
    .Distinct()
    .Count();
我将逐一解释:

强制转换:不能在DataGridViewRowCollectionRows上使用linq,因为它没有实现IEnumerable,所以必须像这样强制转换它

选择:以字符串形式返回每行的第一个单元格

Distinct:仅返回不同的条目,即删除重复项

Count:统计不同条目的数量。

if-else子句不执行任何操作。total++和total=total+1是相同的

因为您只想计算行,如果它没有重复的话。您需要在第一个for循环中移动计数器

    int total = 0;
    for (int currentRow = 0; currentRow < dataGrid_Target.Rows.Count - 1; currentRow++)
    {
        bool IsRowDuplicate = false;
        DataGridViewRow rowToCompare = dataGrid_Target.Rows[currentRow];

        for (int otherRow = currentRow + 1; otherRow < dataGrid_Target.Rows.Count; otherRow++)
        {
            DataGridViewRow row = dataGrid_Target.Rows[otherRow];
            if (!row.IsNewRow)
            {
                if (Convert.ToString(rowToCompare.Cells[0].Value).Equals(Convert.ToString(row.Cells[0].Value)))
                {
                    IsRowDuplicate = true;
                    break;
                }
            }
        }

        if(!IsRowDuplicate)
        {
           total++;
        }
    }
您的if-else子句不起任何作用。total++和total=total+1是相同的

因为您只想计算行,如果它没有重复的话。您需要在第一个for循环中移动计数器

    int total = 0;
    for (int currentRow = 0; currentRow < dataGrid_Target.Rows.Count - 1; currentRow++)
    {
        bool IsRowDuplicate = false;
        DataGridViewRow rowToCompare = dataGrid_Target.Rows[currentRow];

        for (int otherRow = currentRow + 1; otherRow < dataGrid_Target.Rows.Count; otherRow++)
        {
            DataGridViewRow row = dataGrid_Target.Rows[otherRow];
            if (!row.IsNewRow)
            {
                if (Convert.ToString(rowToCompare.Cells[0].Value).Equals(Convert.ToString(row.Cells[0].Value)))
                {
                    IsRowDuplicate = true;
                    break;
                }
            }
        }

        if(!IsRowDuplicate)
        {
           total++;
        }
    }

total++和total=total+1都是相同的东西,那么,我如何才能形成正确的代码?查看我的答案您可以使用Distinct total++和total=total+1都是相同的东西那么,我如何形成正确的代码?查看我的答案您可以使用Distinct