首页 文章

我如何检查DataGridView的重复值并将其计算为一个?

提问于
浏览
0

我有一个带有两个datagridviews的C#应用程序,我想要第一个数据网格来计算位置,并将它放在第二个datagridview中 .

例如:这是我的第一个datagrid InputDataGrid

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

如果我点击一个按钮,我希望这个数据在第二个数据网格中的形式 - > ResultDataGrid

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

这是我的代码: UPDATE

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);
           }
       }
    }

}

2 回答

  • 0

    它有点复杂,但有效:

    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_);
            }
        }
    
  • 0

    我认为最简单的原因是使用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; }
    }
    

相关问题