重写datagridtextboxcolumn实现设置显示与实际列值!(例如性别的显示)

在这里我们来讲一篇关于如何进行数据列内容与显示操作的方法。这种方法类似于实现:有数据表 test(id int not null primary key ,name varchar(20) ,sex bit ), 那么是否有方法不通过直接使用 sql 语句,如: select id ,name ,sex =case sex ( when true then ‘ 男 ’ when false then ‘ 女 ’ else sex end) 的形式来构建要显示的是“男”,“女”,而实际上存储的是 true 和 false 呢?当然,如果我们使用 datagridboolcolumn ,通过设置它的一些属性( truevalue,falsevalue )可以达到类似的效果,但对于那些非 bit 列呢?回答是肯定的。我们使用继承 datagridtextboxcolumn 类,然后重写 GetColumnValueAtRow 方法,来达到效果。 GetColumnValueAtRow 方法,把要从数据源的数据取出,然后判断后,返回我们想要在网格中显示的值。(代码见后面的详细代码)

这样,我们可以顺利地在网格中显示我们想要的数据了,但是还有另外一个问题,就是如果我们想在网格中修改数据,那么是否可以被提交给数据库呢?如果仅仅通过上面的操作,只是达到了显示的目的,还要重写 edit,commit , abort 方法,来达到点击单元格后修改内容,然后提交,最后更新到数据库。

效果图 1

下面是程序的完整代码,这里只是抛砖引玉,希望你可以根据需要来作出相应的改进。

'************************************************************************************

' 程序名称: CanSetValueDatagridTextbox

' 功能说明:继承自 datagridtextboxcolumn 类的列样式,主要实现显示值与实际值的显示与更新

' 参数说明:无

' 返回值 : cansetvaluedatagridtextbox

' 编写人员:闵峰

' 日期时间: 2005-06-16 上午

' 遗留问题:点击列标题排序会发生显示值的改变,这是一个 bug 吗?有待解决

'************************************************************************************

Public Class cansetvaluedatagridtextbox

Inherits System.Windows.Forms.DataGridTextBoxColumn

Region " Windows 窗体设计器生成的代码 "

Public Sub New ()

MyBase .New()

' 该调用是 Windows 窗体设计器所必需的。

InitializeComponent()

' 在 InitializeComponent() 调用之后添加任何初始化

End Sub

'UserControl 重写 dispose 以清理组件列表。

Protected Overloads Overrides Sub Dispose( ByVal disposing As Boolean )

If disposing Then

If Not (components Is Nothing ) Then

components.Dispose()

End If

End If

MyBase .Dispose(disposing)

End Sub

'Windows 窗体设计器所必需的

Private components As System.ComponentModel.IContainer

' 注意:以下过程是 Windows 窗体设计器所必需的

' 可以使用 Windows 窗体设计器修改此过程。

' 不要使用代码编辑器修改它。

  1<system.diagnostics.debuggerstepthrough()> Private  Sub  InitializeComponent() 
  2
  3components =  New  System.ComponentModel.Container() 
  4
  5End  Sub 
  6
  7#  End  Region 
  8
  9Private  rnum  As  Integer  '  列序号 
 10
 11Private  rname  As  String  = ""  '  列名称 
 12
 13Private  m_type  As  Type  '  列的类型 
 14
 15Public  Sub  New  (  ByVal  source  As  DataTable,  ByVal  rnum  As  Integer  ,  ByVal  displaytrue  As  String  ,  ByVal  truevalue  As  String  , _ 
 16
 17ByVal  displayfalse  As  String  ,  ByVal  falsevalue  As  String  ) 
 18
 19rnum = rnum 
 20
 21m_displaytrue = displaytrue 
 22
 23m_valuetrue = truevalue 
 24
 25m_displayfalse = displayfalse 
 26
 27m_valuefalse = falsevalue 
 28
 29m_type = source.Columns(rnum).DataType 
 30
 31End  Sub 
 32
 33Public  Sub  New  (  ByVal  source  As  DataTable,  ByVal  rname  As  String  ,  ByVal  displaytrue  As  String  ,  ByVal  truevalue  As  String  , _ 
 34
 35ByVal  displayfalse  As  String  ,  ByVal  falsevalue  As  String  ) 
 36
 37rname = rname 
 38
 39m_displaytrue = displaytrue 
 40
 41m_valuetrue = truevalue 
 42
 43m_displayfalse = displayfalse 
 44
 45m_valuefalse = falsevalue 
 46
 47m_type = source.Columns(rname).GetType 
 48
 49End  Sub 
 50
 51Private  m_displaytrue  As  String  '  显示的真值 
 52
 53Private  m_displayfalse  As  String  '  显示的假值 
 54
 55Private  m_valuefalse  As  String  '  存储的假值 
 56
 57Private  m_valuetrue  As  String  '  存储的真值 
 58
 59'-------------  以下内容操作数据的显示  \--------------- 
 60
 61'  重写该过程是为了以合适的形式来显示数据(★) 
 62
 63Protected  Overrides  Function  GetColumnValueAtRow(  ByVal  source  As  System.Windows.Forms.CurrencyManager,  ByVal  rowNum  As  Integer  )  As  Object 
 64
 65'  从数据源获得指定行的数据  (  注意:这里使用的类型是  object) 
 66
 67Dim  Result  As  Object  =  MyBase  .GetColumnValueAtRow(source, rowNum) 
 68
 69'  显示的设置 
 70
 71If  Result.ToString = m_valuetrue  Then 
 72
 73Return  Me  .m_displaytrue 
 74
 75ElseIf  Result.ToString = m_valuefalse  Then 
 76
 77Return  Me  .m_displayfalse 
 78
 79ElseIf  Result  Is  DBNull.Value  Then 
 80
 81If  Me  .NullText  Is  Nothing  Then 
 82
 83Return  DBNull.Value 
 84
 85Else 
 86
 87Return  Me  .NullText 
 88
 89End  If 
 90
 91Else 
 92
 93Throw  New  Exception("  该列中存在没有指定显示的字符!  ") 
 94
 95End  If 
 96
 97End  Function 
 98
 99'---------------  以下内容操作更新  \------------------ 
100
101Private  isediting  As  Boolean  =  False  '  是否在修改状态 
102
103Private  oldvalue  As  Object  '  原始值 
104
105'  点击单元格,准备编辑(★) 
106
107Protected  Overloads  Overrides  Sub  Edit(  ByVal  source  As  System.Windows.Forms.CurrencyManager,  ByVal  rowNum  As  Integer  ,  ByVal  bounds  As  System.Drawing.Rectangle,  ByVal  [readOnly]  As  Boolean  ,  ByVal  instantText  As  String  ,  ByVal  cellIsVisible  As  Boolean  ) 
108
109Console.WriteLine("edit")  '--------- 
110
111'  直接触发父类事件,是为了达到点击后选中文本的效果。(如果不触发就要自己把  textbox  移动到对应的边框内) 
112
113MyBase  .Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible) 
114
115'  设置修改码 
116
117isediting =  True 
118
119'  记录原始值 
120
121oldvalue =  Me  .GetColumnValueAtRow(source, rowNum) 
122
123End  Sub 
124
125'  对输入的修改进行提交(★) 
126
127Protected  Overrides  Function  Commit(  ByVal  dataSource  As  System.Windows.Forms.CurrencyManager,  ByVal  rowNum  As  Integer  )  As  Boolean 
128
129Console.WriteLine("in commit")  '--------- 
130
131'  如果修改码为真则意味着修改完毕 
132
133If  Not  isediting  Then  Return  True 
134
135'  获得单元格的内容 
136
137Dim  currentvalue  As  Object  =  Me  .TextBox.Text 
138
139Console.WriteLine(currentvalue)  '--------- 
140
141Try 
142
143currentvalue = setsuitablevalue(currentvalue) 
144
145SetColumnValueAtRow(dataSource, rowNum, currentvalue) 
146
147&lt;p class="MsoNormal" style="MARGIN: 0cm 0c</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus