在 datagridview 中 修改数据后,再从新累加数据,然后插入到数据库中。

时间:2021-12-20 07:20:38

在 datagridview 中 修改数据后,再从新累加数据,然后插入到数据库中。


用的for循环,先以行循环相加,然后在列循环相加,把数据放到table 中

  DataTable dtable = new DataTable();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("a", typeof(decimal));
                    dt.Columns.Add("b", typeof(decimal));
                    dt.Columns.Add("c", typeof(decimal));
                    dt.Columns.Add("d", typeof(decimal));
                    dt.Columns.Add("e", typeof(decimal));
                    dt.Columns.Add("f", typeof(decimal));
                    dt.Columns.Add("h", typeof(decimal));
                    dt.Columns.Add("j", typeof(decimal));
                    for (int i = 1; i < dataGV.Rows.Count; i++)
                    {
                        int sum = 0;
                        for (int j = 2; j < dataGV.ColumnCount; j++)
                        {
                            sum += int.Parse(dataGV.Rows[i].Cells[j].Value.ToString());
                        }
                        dataGV.Rows[i].Cells[0].Value  = sum;
                        dataGV.Rows[i].Cells[1].Value = sum;
                    }
                    for (int k = 0; k < dataGV.ColumnCount; k++)
                    {
                        int lsum = 0;
                        for (int h = 1; h < dataGV.Rows.Count; h++)
                        {
                            lsum += int.Parse(dataGV.Rows[h].Cells[k].Value.ToString());
                        }
                        dataGV.Rows[0].Cells[k].Value  = lsum;
                    }

 

                    for (int i = 0; i < dataGV.Rows.Count; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["a"] = dataGV.Rows[i].Cells[0].Value.ToString();
                        dr["b"] = dataGV.Rows[i].Cells[1].Value.ToString();
                        dr["c"] = dataGV.Rows[i].Cells[2].Value.ToString();
                        dr["d"] = dataGV.Rows[i].Cells[3].Value.ToString();
                        dr["e"] = dataGV.Rows[i].Cells[4].Value.ToString();
                        dr["f"] = dataGV.Rows[i].Cells[5].Value.ToString();
                        dr["h"] = dataGV.Rows[i].Cells[6].Value.ToString();
                        dr["j"] = dataGV.Rows[i].Cells[7].Value.ToString();
                        dt.Rows.Add(dr);
                    }