admin 管理员组

文章数量: 1086019


2024年3月11日发(作者:jsonp为什么不是真正的ajax)

DataGridView控件创建汇总行实例

在使用DataGridView的过程中发现没有合计,总是觉得很不习惯。用单独的

TxtBox做合计又显得整个界面结构有点散。所有根据自己的摸索自己写了一个

DataGridView合计的类文件。

一、先看效果

说明:在一行输入完成后,自动在汇总行上边增加一行空白行。当输入完成

需要汇总的单元后自动更新汇总行的数据。

二、实现步骤:

1、先建立空白WinForm文档,然后创建一个DataGridView控件。点控件右

上边的三角形,在弹出的对话框中点击Add Columns 创建列。如下图:

上图输入完成后要继续创建列点Add按钮,所有列创建完成后点Cancel按

钮完成列的创建。

2、创建一个类文件,取名myDatagridview,类文件中的代码如下:

namespace WinFormsApp2

{

class myDatagridview

{///

/// 设置初始属性

///

///

///

public DataGridView Dt_jb(DataGridView dg) {

serToAddRows = false;

(2);

[1].ReadOnly = true;

[1].lor = ;//设

置背景色

[1].Cells[0].Value = "汇总";

[1]. = new

(cMonospace, 10,

);//设置字体加粗、大小

[1].lor = ;//设

置字体颜色

return dg;

}

///

/// 插入新行

///

///

///

public void dt_cr(DataGridView dgc) {

DataGridViewRow newRow = new DataGridViewRow();

(-1,newRow);

tCell = [ - 2].Cells[0];

}

///

/// 汇总求和

///

///

///

public void SumColumn(DataGridView dgc ,int sc )

{

double sumc = 0;

for (int i = 0; i < -1; i++) {

sumc = sumc + le([i].Cells[sc].Value);

}

[-1].Cells[sc].Value = sumc;

}

}

}

2、选中DataGridView控件,按F7查看代码。在弹出的窗体代码的窗口中输

入以下代码:

using ;

namespace WinFormsApp2

{

public partial class DataGridView测试 : Form

{

myDatagridview m_dv = new myDatagridview();

public DataGridView测试()

{

InitializeComponent();

m__jb(dgv1);

}

private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

if (Index == 6)

{

m__cr(dgv1);

}

}

private void dgv1_CellLeave(object sender, DataGridViewCellEventArgs e)

{

if (Index == 3)

{

t();//必须加,光标离开完成修改,否则获取的是之前的

m_umn(dgv1, Index);

}

if (Index == 5)

{

t();//在离开后立即生效

m_umn(dgv1, Index);

}

}

private void dgv1_CellEnter(object sender, DataGridViewCellEventArgs e)

{

if (Index == 5)

{

t();

if ([ex].Cells[3] != null &&

[ex].Cells[4] != null)

{

[ex].Cells[5].Value =

le([ex].Cells[3].Value) *

le([ex].Cells[4].Value);

}

}

}

}

}


本文标签: 完成 创建 汇总 弹出