datagridviewex最新版_(转)DataGridView多维表头及其扩展功能

private voidForm1_Load(objectsender, EventArgs e)        {            InitDataTable();            InitDataGridView();        }        DataTable table= newDataTable();private voidInitDataTable()        {                      DataColumn col;            col= newDataColumn();            col.ColumnName= "客户名称";            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "产品名称";            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "规格";            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "单位";            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "期初存货数量";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "期初货款";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "期初帐款";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "发货数量";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "发货金额";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "开票数量";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "开票金额";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.ColumnName= "回款数量";            col.DataType=System.Type.GetType("System.Decimal");            table.Columns.Add(col);            col= newDataColumn();            col.DataType=System.Type.GetType("System.Decimal");            col.ColumnName= "回款金额";            table.Columns.Add(col);            col= newDataColumn();            col.DataType=System.Type.GetType("System.Decimal");            col.ColumnName= "未开票回款数量";            table.Columns.Add(col);            col= newDataColumn();            col.DataType=System.Type.GetType("System.Decimal");            col.ColumnName= "未开票回款金额";            table.Columns.Add(col);            col= newDataColumn();            col.DataType=System.Type.GetType("System.Decimal");            col.ColumnName= "期末存货数量";            table.Columns.Add(col);            col= newDataColumn();            col.DataType=System.Type.GetType("System.Decimal");            col.ColumnName= "期末应收货款";            table.Columns.Add(col);            col= newDataColumn();            col.DataType=System.Type.GetType("System.Decimal");            col.ColumnName= "期末应收帐款";            table.Columns.Add(col);        }private voidInitDataGridView()        {            MutilGridHeader topRow= newMutilGridHeader();            topRow.SetRowCol(3,18);//第一行topRow.Cells[0][0].Value= "客户";            topRow.Cells[0][0].RowSpan= 3;            topRow.Cells[0][1].Value= "产品名称";            topRow.Cells[0][1].RowSpan= 3;            topRow.Cells[0][2].Value= "规格";            topRow.Cells[0][2].RowSpan= 3;            topRow.Cells[0][3].Value= "单位";            topRow.Cells[0][3].RowSpan= 3;            topRow.Cells[0][4].Value= "期初";            topRow.Cells[0][4].ColSpan= 3;            topRow.Cells[0][7].Value= "本期";            topRow.Cells[0][7].ColSpan= 8;            topRow.Cells[0][15].Value= "期末";            topRow.Cells[0][15].ColSpan= 3;//第二行topRow.Cells[1][4].Value= "存货数量";            topRow.Cells[1][4].RowSpan= 2;            topRow.Cells[1][5].Value= "应收货款";            topRow.Cells[1][5].RowSpan= 2;            topRow.Cells[1][6].Value= "应收帐款";            topRow.Cells[1][6].RowSpan= 2;            topRow.Cells[1][7].Value= "发货";            topRow.Cells[1][7].ColSpan= 2;            topRow.Cells[1][9].Value= "开票";            topRow.Cells[1][9].ColSpan= 2;            topRow.Cells[1][11].Value= "回款";            topRow.Cells[1][11].ColSpan= 2;            topRow.Cells[1][13].Value= "未开票回款";            topRow.Cells[1][13].ColSpan= 2;            topRow.Cells[1][15].Value= "存货数量";            topRow.Cells[1][15].RowSpan= 2;            topRow.Cells[1][16].Value= "应收货款";            topRow.Cells[1][16].RowSpan= 2;            topRow.Cells[1][17].Value= "应收票款";            topRow.Cells[1][17].RowSpan= 2;//第三行topRow.Cells[2][7].Value= "数量";            topRow.Cells[2][8].Value= "金额";            topRow.Cells[2][9].Value= "数量";            topRow.Cells[2][10].Value= "金额";            topRow.Cells[2][11].Value= "数量";            topRow.Cells[2][12].Value= "金额";            topRow.Cells[2][13].Value= "数量";            topRow.Cells[2][14].Value= "金额";            dataGridViewEx1.Header=topRow;            dataGridViewEx1.DataSource=table;            table.DefaultView.AllowNew= false;            dataGridViewEx1.Columns[0].Width= 120;            dataGridViewEx1.Columns[1].Width= 100;            dataGridViewEx1.Columns[2].Width= 80;for(inti= 2;i<18;i++)                dataGridViewEx1.Columns[i].Width= 60;        }

开发环境为vs2012 C#源码及完全样例 此版本为2012.2.24日更新版本,更新内容为 * 最后修改时间:2012.2.24 增加了汇总信息的自定义控制属性,参见功能描述12 * 2012.2.23 修改了过多列时父标题不显示的问题,重新写了算法,修改了标题头重叠及不能换行的问题,参见说明11. 总功能列举如下: /**************************************************************************************************** * Copyright (C) 2012 明振居士 版权没有,任意拷贝及使用,但对使用造成的任何后果不负任何责任,互相开源影响,共同进步 * 文 件 名:DataGridViewEx.cs * 创 建 人:明振居士 * Email:nzj.163@163.com qq:342155124 * 创建时间:2010-06-01 * 最后修改时间:2012.2.24 增加了汇总信息的自定义控制属性,参见功能描述12 * 2012.2.23 修改了过多列时父标题不显示的问题,重新写了算法,修改了标题头重叠及不能换行的问题,参见说明11. * 2012-1-19 增加第10条所示的功能;修改了列头超过26列的错误,导出excel为数组方式,速度更快,导出的单元格设置为文本格式。 * 标 题:用户自定义的DataGridView控件 * 功能描述:扩展DataGridView控件功能 * 扩展功能: * 1、搜索Search(); 有两个同明方法,参数不同 F3为快捷键继续向下搜索 * 2、用TreeView HeadSource 来设置复杂的标题样式,如果某个节点对应的显示列隐藏,请将该节点Tag设置为hide,隐藏列的排列位置与绑定数据元列位置对应,树叶节点的顺序需要与结果集的列顺序一致 * 3、通过反射导出Excel,无需引用com组件,方法ExportExcel() ,不受列数的限制,表头同样可以导出,AutoFit属性设置导出excel后是否自动调整单元格宽度 * 导出内容支持自定义的:Title List Header List Footer,支持在设计时值的设定,窗口关闭时Excel资源自动彻底释放 * 4、可以自己任意设定那些列显示及不显示,通过调用方法SetColumnVisible()实现。 * 5、设置列标题SetHeader(),设置列永远可见AlwaysShowCols(),设置列暂时不可见HideCols() * 注意,当使用了TreeView作为复杂Header时,不要使用本方法,Header显示的内容根据treeview内容而显示 * 6、列宽度及顺序的保存SaveGridView(),加载LoadGridView() * 7、支持所见即所得的打印功能,举例如下 * private void button5_Click(object sender, EventArgs e) { DGVPrinter printer = new DGVPrinter(); printer.PrintPreviewDataGridView(DataGridViewEx1); } * 8、自定义合并行与列,行合并用 MergeRowColumn 属性,列合并用MergeColumnNames属性,都可以定义多个列 * 9、行标号的设置 bool ShowRowNumber; * 10、增加最后一行的汇总行,支持列的聚合函数,参见http://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.[removed]v=VS.100).aspx * 假设对id列显示“合计”字符,avgPrice进行平均值,total列显示合计,则对ComputeColumns属性增加三行内容:id,合计:;avgPrice,Avg(avgPrice);total,Sum(total) * 如果需要对值进行格式控制,请实现beforeShow事件 * 增加了导出和打印对应的支持,所见即所得的对齐方应用于式导出及打印。 * 11、列标题头自动换行,默认设置为每个列标题头行高为22,如果设置的字体更大,将自动进行计算。如果需要多行显示,请根据需要调节ColumnHeaderHeight属性值, * 其估算方法为预计的标题头层数乘以22,或层数乘以设置的字体高度。 * 12、针对第10条功能,增加汇总信息的自定义控制属性,汇总字体SummaryFont,汇总字体颜色SummaryFontColor,汇总背景色SummaryBGColor,边框与整个表格边框一致 ****************************************************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值