你是否在开发WinForm应用程序时,通常使用DataGrid来作为数据的层现,是否会用到DataGrid分页,添加序号,添加全选,反选功能,有时还会用到不同数据显示不同的颜色.DataGridGost为我们做了这一切,你只需要简单的设置,几乎不用写一行代码就能轻松完成.下面我们一起来进入DataGridGost神奇的世界
[关键词]DataGrid,DataGrid全选,自动添加序号.
1.系统概述
DataGridGos主要是封装了对分页,添加自动编号,添加全选列,转义列(比如,为2时显示加急,3显示不加急),指定条件的行着色等功能,指定条件的行才能被钩选.
2、部份实现代码
添加全选列
{
DataGridColorBoolColumn dgbolChoose_temp;
dgbolChoose_temp = new DataGridColorBoolColumn();
//
// dgbolChoose_temp选择列
//
dgbolChoose_temp.HeaderText = _CheckBoxTitle;
dgbolChoose_temp.MappingName = " NetlmChoose_2006 " ;
dgbolChoose_temp.FalseValue = false ;
dgbolChoose_temp.NullValue = "" ;
dgbolChoose_temp.TrueValue = true ;
dgbolChoose_temp.Width = _ChekcBoxWidth;
int size = _CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles.Count + 1 ;
DataGridColumnStyle[] dgcsarry = new DataGridColumnStyle[size];
dgcsarry[ 0 ] = dgbolChoose_temp;
for ( int i = 0 ; i < size - 1 ; i ++ )
{
dgcsarry[i + 1 ] = _CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles[i];
}
_CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles.Clear();
_CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles.AddRange(dgcsarry);
}
添加自动编号列
{
DataGridColorTextColumn dgSerNum_temp;
dgSerNum_temp = new DataGridColorTextColumn();
dgSerNum_temp.HeaderText = " 序号 " ;
dgSerNum_temp.MappingName = " NetlmSerNumUID " ;
dgSerNum_temp.Alignment = HorizontalAlignment.Center;
dgSerNum_temp.Width = _SerNumWidth;
int size = _CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles.Count + 1 ;
DataGridColumnStyle[] dgcsarry = new DataGridColumnStyle[size];
dgcsarry[ 0 ] = dgSerNum_temp;
for ( int i = 0 ; i < size - 1 ; i ++ )
{
dgcsarry[i + 1 ] = _CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles[i];
}
_CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles.Clear();
_CurrentDataGrid.TableStyles[ 0 ].GridColumnStyles.AddRange(dgcsarry);
}
在对输入的条件直接转换为布尔值时,遇到些困难,开始我想调用CodeDom直接生成表达式的值,但这样做并没有成功。比如 UserName==shumi 其中UserName表示的是列名,需要dr["UserName"].ToString()来读出列的值,而在CodeDom中并不能读出列的值,不知道那位高手有好的解决方案。我的做法是自已写了一个类似编译原理中的词法分析的类,采用四元式的方式分析表达式并求出布尔值,但由于学编译原理的知识差不多全还给老师了,所以在对四元式进行DAG图化简时还存在一定的问题。
下面是部份实现代码:
{
// 将表达式分词
ArrayList ary = StrToBoolExpression.ConvertExpression(str);
for ( int i = 0 ;i < ary.Count;i ++ )
{
if (i < ary.Count - 1 && ! IsOperator(ary[i].ToString()))
{
// 如果是字段,取出字段所表示的值
if (ary[i + 1 ].ToString() != " ! " && ary[i + 1 ].ToString() != " && " && ary[i + 1 ].ToString() != " || " )
{
ary[i] = dr[ary[i].ToString()].ToString();
}
}
}
try
{
return bool .Parse(StrToBoolExpression.ComputePostfix(ary).ToString());
}
catch
{
return false ;
}
}
后记:
这几天感冒了又拉肚子,好难受,从前天晚上都没有吃过任何东西,今天终于吃了两个鸡蛋,身体开始好转了,所以整理了这篇文章。先休息一下,下一篇我将介绍根据DataGridGost自动生成DataGridTableStyle的方法,并对自已开发的这个小工具使用方法做个简单的介绍。这里先提供给大家下载,希望能给大家的开发带来帮助.
文件下载:
类库文档 示例源代码 DataGridTableStyle自动生成工具
接着上午没有写完的说一下DataGridTableStyle自动生成工具的用法
软件界面如上图所示
设置好数据库连接后单击连接按扭,将加载当前数据库中的所有表和字段注释.在左边钩选需要生成的表,如果需要将,整型字段转义(1,显示为男,2显示为女)就在相应的列名行钩选转义.在这里我设定主键是不能为转义列的
上面的显示就是用到了DataGridGhost
生成的代码如下:
1
//作者:舒密 http://netlm.cnblogs.com2
//广西师范学院 TCN工 作室3

4
using System;5
using System;6
using System.Collections;7
using System.ComponentModel;8
using System.Drawing;9
using System.Data;10
using System.Windows.Forms;11
using Netlm.DataGridColumnGost;12
13
namespace DataTableStyles14


{15

/**//// <summary>16
/// RAccountRolesTableStyles 17
/// </summary>18
public class RAccountRolesTableStyles : System.Windows.Forms.DataGridTableStyle19

{20
private System.ComponentModel.Container components = null;21
public RAccountRolesTableStyles()22

{23
InitializeComponent();24
}25

/**//// <summary> 26
/// 清理所有正在使用的资源。27
/// </summary>28
protected override void Dispose( bool disposing )29

{30
if( disposing )31

{32
if(components != null)33

{34
components.Dispose();35
}36
}37
base.Dispose( disposing );38
}39
private DataGridColorTextColumn dgColID;40
private DataGridColorTextColumn dgColAccountGuid;41
private DataGridColorTextColumn dgColRoleGuid;42

43
public void InitializeComponent()44

{45
this.dgColID = new DataGridColorTextColumn();46
this.dgColAccountGuid = new DataGridColorTextColumn();47
this.dgColRoleGuid = new DataGridColorTextColumn();48
49

50
//51
//dgColID 用户角色关系表ID自动增加,52
//53
dgColID.Format ="";54
dgColID.FormatInfo=null;55
dgColID.HeaderText ="用户角色关系表ID自动增加,";56
dgColID.MappingName ="ID";57
dgColID.Width =75;58
//59
//dgColAccountGuid 用户GUID60
//61
dgColAccountGuid.Format ="";62
dgColAccountGuid.FormatInfo=null;63
dgColAccountGuid.HeaderText ="用户GUID";64
dgColAccountGuid.MappingName ="AccountGuid";65
dgColAccountGuid.Width =75;66
//67
//dgColRoleGuid 角色GUID68
//69
dgColRoleGuid.Format ="";70
dgColRoleGuid.FormatInfo=null;71
dgColRoleGuid.HeaderText ="角色GUID";72
dgColRoleGuid.MappingName ="RoleGuid";73
dgColRoleGuid.Width =75;74
// 75
// DataTableStyles76
// 77

this.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[]
{78
this.dgColID,79
this.dgColAccountGuid,80
this.dgColRoleGuid81
});82
}83
}84
}
本文介绍了DataGridGost组件的使用方法,包括如何轻松实现DataGrid的分页、自动添加序号、添加全选列等功能,同时分享了在开发过程中的一些心得与技巧。

被折叠的 条评论
为什么被折叠?



