为WinForm中的datagridview添加统计行

利用以下代码,通过Label控件,直接为DataGridView添加行统计。

1 /// <summary>
2 /// 添加统计行
3 /// </summary>
4 /// <param name="dataGridView"></param>
5 public void AddLable(DataGridView dataGridView)
6         {
7             Label lblParent, lblChild;
8             DataGridViewColumn column;
9 int height = dataGridView.Height;
10 int width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None);
11 int scrollbarheight = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) > dataGridView.Width ? 16 : 0;//水平滚动条高
12 int rowheaderswidth = dataGridView.RowHeadersVisible ? dataGridView.RowHeadersWidth : 0;//行标题宽度
13 int length = rowheaderswidth;
14
15 if (dataGridView.Controls[dataGridView.Name + "____"] != null)
16                 dataGridView.Controls.Remove(dataGridView.Controls[dataGridView.Name + "____"]);
17
18             lblParent = new Label();
19             lblParent.Name = dataGridView.Name + "____";
20             lblParent.BackColor = Color.Silver;
21             lblParent.Left = 1;
22             lblParent.Top = height - scrollbarheight - 23 - 1;
23             lblParent.Height = 23;
24             lblParent.Width = width + rowheaderswidth - 1;
25             dataGridView.Controls.Add(lblParent);
26
27 for (int i = 0; i < dataGridView.Columns.Count; i++)
28             {
29                 column = dataGridView.Columns[i];
30
31 if (column.Visible)
32                 {
33                     lblChild = new Label();
34                     lblChild.Name = column.Name + "____";
35                     lblChild.BackColor = Color.Transparent;
36                     lblChild.AutoSize = true;
37                     lblChild.Left = length + ((int)column.Width / 2) - 10 + 1;
38                     length += column.Width;
39                     lblChild.Top = 3;
40                     lblParent.Controls.Add(lblChild);
41                 }
42             }
43
44         }

运行效果:

29012010169

获取列数总和,以进行统计:

1 /// <summary>

2 /// 获取列总和
3 /// </summary>
4 /// <param name="dataGridView"></param>
5 /// <param name="columnName"></param>
6 public void SetSUM(DataGridView dataGridView, string columnName)
7         {
8 if (dataGridView.Controls[dataGridView.Name + "____"] != null)
9             {
10 decimal sum = 0;
11
12 for (int i = 0; i < dataGridView.Rows.Count; i++)
13                 {
14 try
15                     {
16 if (dataGridView.Rows[i].Cells[columnName].Value != null)
17                         {
18 //sum += decimal.Parse(dataGridView.Rows[i].Cells[columnName].Value.ToString());
19                             sum += 1; //循环统计
20                         }
21                     }
22 catch { }
23                 }
24
25                 dataGridView.Controls[dataGridView.Name + "____"].Controls[columnName + "____"].Text = sum.ToString();
26             }
27         }

写入户数统计:

1 /// <summary>
2 /// 写入户数
3 /// </summary>
4 /// <param name="dataGridView"></param>
5 /// <param name="columnName"></param>
6 public void SetHu(DataGridView dataGridView, string columnName)
7         {
8 if (dataGridView.Controls[dataGridView.Name + "____"] != null)
9             {
10
11                 dataGridView.Controls[dataGridView.Name + "____"].Controls[columnName + "____"].Text = "户数统计:";
12             }
13         }

你可能感兴趣的:(datagridview)