winfrom C#gridview 怎么实现最后一行合计

2024-11-27 10:22:25
推荐回答(2个)
回答1:

  DataTable dt = getData(); //取得数据
            DataView dv= dt.DefaultView;
            dv.AddNew();
            dataGridView1.DataSource = dv;
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = "合计";
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
            for (int i = 1; i < dataGridView1.Columns.Count; i++) 
            {
                decimal _count = 0;
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                     decimal _ResultNum = 0;
                     if (!decimal.TryParse(dataGridView1.Rows[j].Cells[i].Value.ToString(), out _ResultNum))
                     { 
                         continue;
                     }
                     _count += _ResultNum;
                }
                dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[i].Value =_count;
            }

回答2:

看具体的需求,可以实现,私聊