Windows应用程序的datagridview [英] datagridview of windowsapplication

查看:86
本文介绍了Windows应用程序的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用.net的Windows应用程序的datagridview在页脚中添加总和

how to add sum in footer using datagridview of windowsapplication of .net

推荐答案

尝试以下操作:

http://support.microsoft.com/kb/836672 [
try this:

http://support.microsoft.com/kb/836672[^]


最好的方法是在表单中添加文本框和标签,然后将它们放置在datagridview下方,使它们看起来像网格的一部分.然后,您可以使用CellPainting事件来计算总数并更新文本框.这是一些将控件放置在网格下方,然后自动计算总数的代码.假设您的表单有一个名为dgv1的datagridview,一个名为txtTotal的文本框和一个名为lblTotal
的标签
The best idea is to add a textbox and a label to your form, and then position them below the datagridview such that they look like part of the grid. Then you can use the CellPainting event to calculate the total and update the textbox. Here is some code that will position the controls below the grid, and then automatically calculate the total. This assumes your form has a datagridview named dgv1, a textbox named txtTotal and a label named lblTotal

public Form2()
       {
           InitializeComponent();
           PositionTotalControls();
       }
       void PositionTotalControls()
       {
           lblTotal.Text = "Total";
           lblTotal.Height = txtTotal.Height;
           lblTotal.AutoSize = false;
           lblTotal.TextAlign = ContentAlignment.MiddleCenter;
           int X = this.dgv1.GetCellDisplayRectangle(0, -1, true).Location.X;
           lblTotal.Width = this.dgv1.Columns[0].Width + X;
           lblTotal.Location = new Point(0, this.dgv1.Height - txtTotal.Height);
           this.dgv1.Controls.Add(lblTotal);
           txtTotal.Width = this.dgv1.Columns[1].Width;
           X = this.dgv1.GetCellDisplayRectangle(1, -1, true).Location.X;
           txtTotal.Location = new Point(X, this.dgv1.Height - txtTotal.Height);
           this.dgv1.Controls.Add(txtTotal);
           this.dgv1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
       }
       void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
       {
           int sum = 0;
           for (int i = 0; i < this.dgv1.Rows.Count; i++)
           {
               sum += Convert.ToInt32(this.dgv1[1, i].Value);
           }
           txtTotal.Text = sum.ToString();
           int X = this.dgv1.GetCellDisplayRectangle(0, -1, true).Location.X;
           lblTotal.Width = this.dgv1.Columns[0].Width + X;
           lblTotal.Location = new Point(0, this.dgv1.Height - txtTotal.Height);
           txtTotal.Width = this.dgv1.Columns[1].Width;
           X = this.dgv1.GetCellDisplayRectangle(1, -1, true).Location.X;
           txtTotal.Location = new Point(X, this.dgv1.Height - txtTotal.Height);
       }



这也假定您的datagridview中有两列,并且将标签自动定位在第一列下方,并将文本框放置在第二列下方.它将自动合计第二列.

希望对您有帮助



This also assumes you have two columns in your datagridview, and will automatically position the label below the first column and the textbox below the second column. It will automatically total the second column.

Hope this helps


这篇关于Windows应用程序的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆