文件名称:datagridview加入合计行
文件大小:2KB
文件格式:TXT
更新时间:2012-01-24 14:07:40
datagridview 合计
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“sepaDB_SysDataSet.TrueNewChem”中。您可以根据需要移动或移除它。 this.trueNewChemTableAdapter.Fill(this.sepaDB_SysDataSet.TrueNewChem); DataRow dr = this.sepaDB_SysDataSet.TrueNewChem.NewRow(); object objSum = this.sepaDB_SysDataSet.TrueNewChem.Compute("Sum(count)", null); dr["count"] = objSum.ToString(); dr["F单位名称"] = "合计:"; this.sepaDB_SysDataSet.TrueNewChem.Rows.Add(dr); trueNewChemBindingSource.Sort = "count desc"; } private void trueNewChemDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (e.RowIndex >= trueNewChemDataGridView.Rows.Count - 1) return; DataGridViewRow dgr = trueNewChemDataGridView.Rows[e.RowIndex]; try { //dgr.Cells[0]是当前性别列的索引值,用以确定判断哪一列的值 if (dgr.Cells[0].Value.ToString() == "合计:") { //定义画笔,使用颜色是深灰。 using (SolidBrush brush = new SolidBrush(Color.Red )) { //利用画笔填充当前行 e.Graphics.FillRectangle(brush, e.RowBounds); //将值重新写回当前行。 e.PaintCellsContent(e.ClipBounds); e.Handled = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }