欢迎光临

木子屋

标题: DataGridView底部添加统计行示例

作者: dnawo

日期: 2009-10-23 06:17 PM

分类: Win编程

内容: 效果图：
相关代码：
复制内容到剪贴板 程序代码using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataBindingComplete += delegate
{
AddLable(dataGridView1);
SetSUM(dataGridView1, "PriceSUM");
};
dataGridView1.ColumnWidthChanged += delegate
{
AddLable(dataGridView1);
SetSUM(dataGridView1, "PriceSUM");
};
using (SqlConnection conn = new SqlConnection("server=(local);database=Test;user id=sa;password=sa;"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("Select top 10 * FROM Products", conn))
{
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
}
}
/// <summary>
/// 添加统计行
/// </summary>
/// <param name="dataGridView"></param>
public void AddLable(DataGridView dataGridView)
{
Label lblParent, lblChild;
DataGridViewColumn column;
int height = dataGridView.Height;
int width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None);
int scrollbarheight = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) > dataGridView.Width ? 16 : 0;//水平滚动条高
int rowheaderswidth = dataGridView.RowHeadersVisible ? dataGridView.RowHeadersWidth : 0;//行标题宽度
int length = rowheaderswidth;
//
if (dataGridView.Controls[dataGridView.Name + "____"] != null)
dataGridView.Controls.Remove(dataGridView.Controls[dataGridView.Name + "____"]);
lblParent = new Label();
lblParent.Name = dataGridView.Name + "____";
lblParent.BackColor = Color.Yellow;
lblParent.Left = 1;
lblParent.Top = height - scrollbarheight - 23 - 1;
lblParent.Height = 23;
lblParent.Width = width + rowheaderswidth - 1;
dataGridView.Controls.Add(lblParent);
for (int i = 0; i < dataGridView.Columns.Count; i++)
{
column = dataGridView.Columns[i];
if (column.Visible)
{
lblChild = new Label();
lblChild.Name = column.Name + "____";
lblChild.BackColor = Color.Transparent;
//lblChild.Text = lblChild.Name;
lblChild.AutoSize = true;
lblChild.Left = length + ((int)column.Width / 2) - 10 + 1;
length += column.Width;
lblChild.Top = 3;
lblParent.Controls.Add(lblChild);
}
}
}
/// <summary>
/// 获取列总和
/// </summary>
/// <param name="dataGridView"></param>
/// <param name="columnName"></param>
public void SetSUM(DataGridView dataGridView, string columnName)
{
if (dataGridView.Controls[dataGridView.Name + "____"] != null)
{
decimal sum = 0;
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
try
{
if (dataGridView.Rows[i].Cells[columnName].Value != null)
{
sum += decimal.Parse(dataGridView.Rows[i].Cells[columnName].Value.ToString());
}
}
catch { }
}
dataGridView.Controls[dataGridView.Name + "____"].Controls[columnName + "____"].Text = sum.ToString();
}
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.188 ms

返回

标题: DataGridView底部添加统计行示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.188 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.266 ms

返回