datagridview添加汇总行


1、定义一个累加变量,累计需要汇总的列: decimal a=0.00m;
 
2、将要显示在datagridview中的数据放到DataTable dt对象中去。chxclass.selectdj1()是类中的一个方法,返回dataset对象。
  
        System.Data.DataTable dt=chxclass.selectdj1().Tables[0];
3、在循环中,累加第五列的值。
        for(int i=0;i<dt.Rows.Count;i++)  
              { 
                a+=Convert.ToDecimal(dt.Rows[i][4]);
              }


4、在dt中增加一行,第一列显示合计,第五列需要求和的列赋值为a。 
             DataRow   dr=dt.NewRow();  
             dr[0]="合计";  
             dr[4]=a;
             dt.Rows.Add(dr); 


5、设置dataGridView的数据源。
             dataGridView1.DataSource=dt;


这样就可以在dataGridView中最好一行增加合计行。

在.NET的Windows Forms应用程序中,DataGridView控件用于显示和编辑数据。若要在DataGridView添加一个倒序的号,可以采取以下步骤: 1. 创建一个新的列来存储倒序号。可以通过DataGridView的Columns属性添加一列,设置其类型为自定义,并将此列固定在最左侧,以便它始终显示在最前面。 2. 设置该列的名称,例如可以命名为"倒序号"。 3. 为该列的每一计算号。由于需要倒序显示,可以通过计算DataGridView的总数减去当前号再加1来得到。具体可以使用`TotalRowCount - row.Index`的公式,其中`TotalRowCount`是DataGridView中总数,`row.Index`是当前的索引(从0开始)。 4. 将计算得到的倒序号赋值给新列的单元格。 示例代码如下: ```csharp // 假设dgwYourDataGridView是你的DataGridView控件实例 DataGridViewColumn colReverseIndex = new DataGridViewColumn(); colReverseIndex.Name = "倒序号"; colReverseIndex.HeaderText = "倒序号"; colReverseIndex.FillWeight = 10; // 设置列宽度 colReverseIndex.ReadOnly = true; // 设置为只读 colReverseIndex.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; // 添加列到DataGridView dgwYourDataGridView.Columns.Insert(0, colReverseIndex); // 计算倒序号并显示 for (int i = 0; i < dgwYourDataGridView.Rows.Count; i++) { int reverseIndex = dgwYourDataGridView.TotalRowCount - i; dgwYourDataGridView.Rows[i].Cells["倒序号"].Value = reverseIndex.ToString(); } ``` 注意,这里假设你已经设置了DataGridView的数据源,并且在初始化时添加了倒序号列。如果数据是在运时动态添加的,你可能需要在添加事件中更新这个倒序号列。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值