【问题标题】:How I can check the DataGridView for duplicate values and calculate this to one?如何检查 DataGridView 中的重复值并将其计算为一个?
【发布时间】:2014-11-17 17:42:55
【问题描述】:

我有一个带有两个数据网格视图的 C# 应用程序,我希望第一个数据网格计算位置并将其放在第二个数据网格视图的两个位置。

例如:这是我的第一个数据网格InputDataGrid

Position | Description | Value
-------------------------------
001      | test        | 2,5 
002      |             | 1
001      | hello       | 1,5
002      | test2       | 2  

如果我单击一个按钮,我希望此数据在此表单的第二个数据网格中 -> ResultDataGrid

Position | Value
----------------
001      | 4
002      | 3

这是我的代码:更新

private void btnCalc_Click(object sender, EventArgs e) {

    foreach (DataGridViewRow row in InputDataGrid.Rows)
    {
        if (row.Cells[0].Value == null)
        {
            break;
        }

           string position = (string)row.Cells[0].Value;
           Double sellvalue = Convert.ToDouble(row.Cells[2].Value);

       foreach (DataGridViewRow resultrow in ResultDatagrid.Rows)
       {
           if ((string)resultrow.Cells[0].Value == position)
           {
               Double oldvalue = Convert.ToDouble(resultrow.Cells[1].Value);
               Double newValue = oldvalue + sellvalue;

               resultrow.Cells[0].Value = Convert.ToString(newValue);
           }
           else 
           {
               resultrow.Cells[0].Value = position;
               resultrow.Cells[1].Value = Convert.ToString(sellvalue);
               ResultDatagrid.Rows.Add(resultrow);
           }
       }
    }

}

【问题讨论】:

  • 从你的问题中看不清楚你想要达到什么或者你有什么问题,请解释一下。
  • 我希望我的应用程序计算相同的位置并将其显示在第二个 datagridview 中
  • 和什么位置一样?为什么您无法做到这一点,您需要帮助解决什么问题?

标签: c# winforms visual-studio-2010 datagrid cells


【解决方案1】:

我认为最简单的原因是使用 linq 只用几行代码对 DataGridView 的底层数据源执行求和运算,然后将结果用作另一个网格的数据源:

public class Program
{
    public static void Main()
    {
        var items = new List<DataItem>
        {
            new DataItem { Position = "001", Description = "test", Value = 2.5m },
            new DataItem { Position = "002", Description = "", Value = 1m },
            new DataItem { Position = "001", Description = "hello", Value = 1.5m },
            new DataItem { Position = "002", Description = "test2", Value = 2m }
        };

        var results = 
            items
            .GroupBy(x => x.Position, (key, values) => new { Position = key, Values = values })
            .Select(g => new DataItem() 
            { 
                Position = g.Position, 
                Value = g.Values.Sum(x => x.Value) 
            });
    }
}

public class DataItem
{
    public string Position { get; set; }
    public string Description { get; set; }
    public decimal Value { get; set; }
}

【讨论】:

    【解决方案2】:

    这有点复杂但有效:

    private void button2_Click(object sender, EventArgs e)
        {
            //Contains the text of column0 e.g 001, 002 etc..
            List<string> myListCol0Text = new List<string>();
            //Contains the row index e.g 0, 1 ,2 ,3 etc...
            List<int> myListCol0Index = new List<int>();
            //Contains the strings of the resulting column0 datagrid
            List <string> col0 = new List<string>();
            //Contains the doubles of the resulting column2 datagrid
            List <double> col2 = new List<double>();
            int i = 0, k = 0;
    
            foreach (DataGridViewRow row in InputDataGrid.Rows)
            {
                if (row.Cells[0].Value == null)
                {
                    break;
                }
    
                //Fill two lists with data
                myListCol0Text.Add(row.Cells[0].Value.ToString ());
                myListCol0Index.Add(i);
    
                i++;
            }
    
            double dbl = 0D;
            int count = 0;
            bool bl = false ;
    
            while (myListCol0Text.Count != 0)
            {
                count = myListCol0Text.Count;
                k = 0;
                bl = false;
                dbl = 0D;
                //Compares the first element of myListCol0Text with ALL the next ones.
                //If it finds a dublicate it removes it and in the end removes the first
                //element also
                for (i = 0; i < count - 1; i++)
                {
                    if (myListCol0Text[0] == myListCol0Text[k + 1]) //Dublicate
                    {
                        if(!bl) //Only the first dublicate is added to col0 list
                        {
                            col0.Add(myListCol0Text[0].ToString());
                            dbl = Convert.ToDouble(dataGridView1.Rows[myListCol0Index[0]].Cells[2].Value);
                            dbl += Convert.ToDouble(dataGridView1.Rows[myListCol0Index[k + 1]].Cells[2].Value);
                            col2.Add(dbl);
                            bl = true;
                        }
                        else{ //dont add to list but make the addition of the first and third column
                            dbl += Convert.ToDouble(dataGridView1.Rows[myListCol0Index[k + 1]].Cells[2].Value);
                            col2[col2.Count -1] = dbl;
                        }
    
                        //Removes dublicate
                        myListCol0Text.RemoveAt(k + 1);
                        myListCol0Index.RemoveAt(k + 1);
                    }
                    else
                    {
                        k++;
                    }
                }
    
                //Removes first element
                myListCol0Text.RemoveAt(0);
                myListCol0Index.RemoveAt(0);
            }
    
            //Add col0 and col2 to the resulting datagrid
            DataGridViewRow row_;
    
            for (i = 0; i < col0.Count; i++)
            {
                row_ = new DataGridViewRow();
    
                row_.CreateCells(ResultDatagrid);
                row_.Cells[0].Value = col0[i];
                row_.Cells[1].Value = col2[i].ToString ();
    
                ResultDatagrid.Rows.Add(row_);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多