winform DataGridView添加合计行
使用方法
/*
DataTable dt= DBUtility.DB.FromSql(sql).ToDataTable();
DataGridViewAddSumRow sumRow = new DataGridViewAddSumRow();
sumRow.Xh_field = "xuhao";
sumRow.DgvName = this.dataGridView1;
sumRow.dataTableName = dt;
sumRow.begin();
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
namespace YL.Code
{
public class DataGridViewAddSumRow
{
private DataGridView dgv = null;
private DataTable dt = null;
private string xh_field = "";
public string Xh_field
{
get { return xh_field; }
set { xh_field = value; }
}
public DataGridViewAddSumRow()
{
}
/// <summary>
/// 设置表格的数据源
/// </summary>
public DataTable dataTableName
{
set
{
this.dt = value;
}
}
/// <summary>
///传递表格的名称
/// </summary>
public DataGridView DgvName
{
set
{
dgv = value;
}
}
/// <summary>
/// 开始添加合计行
/// </summary>
public void begin()
{
initDgv();
}
private void initDgv()
{
if (dgv != null)
{
this.dgv.DataSourceChanged += new EventHandler(dataGridView_DataSourceChanged);
this.dgv.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseClick);
this.dgv.CellValueChanged += new DataGridViewCellEventHandler(dataGridView_CellValueChanged);
this.dgv.AllowUserToAddRows = false;
dgv.DataSource = dt;
}
}
/// <summary>
/// 计算合计算
/// </summary>
/// <param name="dgv">要计算的DataGridView</param>
private void SumDataGridView(DataGridView dgv)
{
if (dgv.DataSource == null) return;
//DataTable dt = (DataTable)dgv.DataSource;
if (dt.Rows.Count < 1) return;
decimal[] tal = new decimal[dt.Columns.Count];
DataRow ndr = dt.NewRow();
string talc = "";
int number = 1;
foreach (DataRow dr in dt.Rows)
{
dr[xh_field] = number++;
int n = 0;
foreach (DataColumn dc in dt.Columns)
{
if (talc == "" && dc.DataType.Name.ToUpper().IndexOf("STRING") >= 0)
{ talc = dc.ColumnName; }
if (dc.DataType.IsValueType)
{
string hej = dr[dc.ColumnName].ToString();
try
{
if (hej != string.Empty) tal[n] += decimal.Parse(hej);
}
catch (Exception) { }
//if (hej != string.Empty) tal[n] += decimal.Parse(hej);
}
n++;
}
}
ndr.BeginEdit();
for (int i = 0; i < dt.Columns.Count; i++)
{
if (tal[i] != 0)
ndr[i] = tal[i];
}
ndr[xh_field] = ((int)(dt.Rows.Count + 1)).ToString();
if (talc != "") ndr[talc] = "合计";
ndr.EndEdit();
dt.Rows.Add(ndr);
dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.BackColor = Color.FromArgb(255, 222, 210);
dgv.Rows[dgv.Rows.Count - 1].ReadOnly = true;
if (dgv.Tag == null)
{
foreach (DataGridViewColumn dgvc in dgv.Columns)
{
dgvc.SortMode = DataGridViewColumnSortMode.Programmatic;
}
}
dgv.Tag = ndr;
}
private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridView sortDgv = (DataGridView)sender;
int fx = 0;
if (sortDgv.AccessibleDescription == null)
{
fx = 1;
}
else
{
fx = int.Parse(sortDgv.AccessibleDescription);
fx = (fx == 0 ? 1 : 0);
}
sortDgv.AccessibleDescription = fx.ToString();
if (sortDgv.Columns[e.ColumnIndex].Name == xh_field) return;
DataGridViewColumn nCol = sortDgv.Columns[e.ColumnIndex];
if (nCol.DataPropertyName == string.Empty) return;
if (nCol != null)
{
sortDgv.Sort(nCol, fx == 0 ? ListSortDirection.Ascending : ListSortDirection.Descending);
}
//--
DataRow dr = (DataRow)sortDgv.Tag;
DataTable dt = (DataTable)sortDgv.DataSource;
DataRow ndr = dt.NewRow();
ndr.BeginEdit();
for (int i = 0; i < dt.Columns.Count; i++)
{
ndr[i] = dr[i];
}
dt.Rows.Remove(dr);
//if (e.ColumnIndex != 0)
{
int n = 1;
for (int i = 0; i < sortDgv.Rows.Count; i++)
{
DataGridViewRow dgRow = sortDgv.Rows[i];
DataRowView drv = (DataRowView)dgRow.DataBoundItem;
DataRow tdr = drv.Row;
tdr.BeginEdit();
tdr[xh_field] = n;
n++;
tdr.EndEdit();
}
sortDgv.Refresh();
sortDgv.RefreshEdit();
}
ndr[xh_field] = ((int)(dt.Rows.Count + 1)).ToString();
ndr.EndEdit();
dt.Rows.Add(ndr);
sortDgv.Tag = ndr;
//--
sortDgv.Sort(sortDgv.Columns[xh_field], ListSortDirection.Ascending);
sortDgv.Columns[xh_field].HeaderCell.SortGlyphDirection = SortOrder.None;
nCol.HeaderCell.SortGlyphDirection = fx == 0 ? SortOrder.Ascending : SortOrder.Descending;
sortDgv.Rows[sortDgv.Rows.Count - 1].DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 210);
}
private void dataGridView_DataSourceChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//DataTable dt = (DataTable)dgv.DataSource;
if (dt == null) return;
decimal[] tal = new decimal[dt.Columns.Count];
if (dt.Columns.IndexOf(xh_field) < 0)
{
DataColumn dc = new DataColumn(xh_field, System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
dgv.Columns[xh_field].DisplayIndex = 0;
dgv.Columns[xh_field].HeaderText = "序号";
dgv.Columns[xh_field].SortMode = DataGridViewColumnSortMode.Programmatic;
dgv.AutoResizeColumn(dgv.Columns[xh_field].Index);
dgv.Columns[xh_field].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgv.Columns[xh_field].Visible = true;
}
SumDataGridView(dgv);
}
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Tag == null || e.RowIndex < 0 || e.RowIndex == dgv.Rows.Count - 1) return;
string col = dgv.Columns[e.ColumnIndex].DataPropertyName;
if (col == string.Empty) return;
if (((DataRowView)dgv.Rows[e.RowIndex].DataBoundItem).Row.Table.Columns[col].DataType.IsValueType)
{
decimal tal = 0;
foreach (DataGridViewRow dgvr in dgv.Rows)
{
if (dgvr.Index != dgv.Rows.Count - 1)
{
string hej = dgvr.Cells[e.ColumnIndex].Value.ToString();
if (hej != string.Empty) tal += decimal.Parse(hej);
}
}
if (tal == 0)
dgv[e.ColumnIndex, dgv.Rows.Count - 1].Value = DBNull.Value;
else
dgv[e.ColumnIndex, dgv.Rows.Count - 1].Value = tal;
}
}
}
}
winform DataGridView添加合计行的更多相关文章
- KDTable如何添加合计行?
/** * 功能:添加合计行 * * @param table * 指定的KDTable * @param fields * 需要合计的列 */ public static void apendFoo ...
- C# DataGridView添加新行的2个方法
可以静态绑定数据源,这样就自动为DataGridView控件添加 相应的行.假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行 ...
- Winform DataGridView添加列头checkbox
using System; using System.Data; using System.Drawing; using System.Windows.Forms; using Yanwen.Logi ...
- Gridview里添加合计行
ShowFooter="true" ; ; ; protected void gvIncomeYG_RowDataBound(object sender, GridViewRowE ...
- jqGrid 添加 合计行 footDate
jQuery(table_id).jqGrid({ url : url,//组件创建完成之后请求数据的url datatype : "json",//请求数据返回的类型.可选jso ...
- 分享一个带有合计行功能的DataGridView扩展
因为一个Winform的项目中需要用到带有合计行的表格,并且需要满足以下需求: 合计行可自动对需要求和的列进行求和计算; 合计行必须固定(冻结)在表格的最底部,且其位置不受滚动条的滚动而移动; 可以设 ...
- C#给DataTable添加序号、C#给DataTable添加合计、小计
/// <summary> /// 给DataTable添加序号 /// </summary> /// <param name= ...
- FineUI大版本升级,外置ExtJS库、去AXD化、表格合计行、表格可编辑单元格的增删改、顶部菜单框架
这是一篇很长的文章,在开始正文之前,请允许我代表目前排名前 20 中唯一的 .Net 开源软件 FineUI 拉下选票: 投票地址: https://code.csdn.net/2013OSSurve ...
- Winform中的dataGridView添加自动编号
1.Winform中的dataGridView添加自动编号:http://blog.csdn.net/ohyoyo2014/article/details/38346887 2.如何为datagrid ...
随机推荐
- mysql服务器系统优化
1.选择合适的IO调度 对于mysql的系统,如果是SSD,那么应该使用NOOP调度算法,如果是磁盘,就应该使用Deadline调度算法.默认是CFQ echo dealine > /sys/b ...
- WPS去掉英语单词下面的红斜线
我们在使用WPS的时候,经常会用到英语但是,但是在编码的时候,有些单词是缩写形成的,WPS就会自动验证,产生红色波浪线,提示我们单词写错的问题,那看起来就显得很不美观别扭 那么我们不想要这个红斜杠,怎 ...
- 页面中插入视频兼容ie8以上的浏览器
有时候页面中需要插入视频,如果不考虑ie8的话:就是直接用h5标签<video></video>就可以了: 但是有的时候需求是需要考虑ie8,这样我们就可以用插件,这种vide ...
- linux 命令——5 rm(转)
昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所 ...
- 2018.6.1 oracle数据库乱码问题
执行select * from v$controlfile;为什么结果是未选定行? 显示连接了但是select * from dba_data_files; 显示中文乱码: shutdown star ...
- jstree前端设置默认选中项
$("#jstree").on("loaded.jstree", function (event, data) { var currDeptId = crm.g ...
- jQuery Pagination分页插件--无刷新
源码:https://github.com/SeaLee02/FunctionModule/blob/master/UploadFiles/WebDemo/FenYE/FenYeAjax.aspx 代 ...
- Vue3.0脚手架搭建
https://www.jianshu.com/p/fbcad30031c2 vue3.0官网:https://cli.vuejs.org/zh/guide/ 介绍: notice: 这份文档是对应 ...
- JZOJ 3385. 【NOIP2013模拟】黑魔法师之门
3385. [NOIP2013模拟]黑魔法师之门 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limi ...
- BootCDN 开源项目免费 CDN 加速服务,Jquery插件库
2017-11-17 19:38:32 免费好用的在线 css js 文件引用 BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务 Jquery插件库 .