胖在一方

出得厅堂入得厨房的胖子

导航

CustomDataGridTextBoxColumn

Posted on 2006-09-12 17:12  胖在一方  阅读(390)  评论(0)    收藏  举报

 

 1 同事写的一个class
 2 '================================================================

 3 '
 4 'Who: liuzong

 5 '日期: 2002/06/14
 6 '目的:解决Windows下面DataGrid整行选中问题
 7 '
 8 '=================================================================

 9 
10 Imports System
11 Imports
 System.ComponentModel
12 Imports
 System.Collections
13 Imports
 System.Diagnostics
14 Imports
 System.Drawing
15 

16 Public Class CustomDataGridTextBoxColumn
17     Inherits
 System.Windows.Forms.DataGridTextBoxColumn
18 

19     Public Sub New(ByVal HeaderText As StringByVal MappingName As StringByVal Width As Integer)
20         Me.HeaderText =
 HeaderText
21         Me.MappingName =
 MappingName
22         Me.Width =
 Width
23         Me.NullText = String
.Empty
24     End Sub

25 
26     '这里没有增加代码,WHY:因为DataGrid属性,ReaderOnly=True
27     Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerByVal bounds As System.Drawing.Rectangle, ByVal [readOnlyAs BooleanByVal instantText As StringByVal cellIsVisible As Boolean)
28 

29     End Sub
30 
31     '重载DataGrid单元格,使选择一行时能整行选中
32     Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
33         Dim bVal As Object =
 GetColumnValueatrow(source, rowNum)
34         If Me.DataGridTableStyle.DataGrid.CurrentRowIndex = rowNum Then

35             g.FillRectangle(New SolidBrush(Me.DataGridTableStyle.SelectionBackColor), bounds)
36             If IsDBNull(bVal) Then

37                 g.DrawString(String.Empty, Me.DataGridTableStyle.DataGrid.Font, New SolidBrush(Me.DataGridTableStyle.SelectionForeColor), bounds.X + 2, bounds.Y + 2)
38             Else

39                 g.DrawString(CType(bVal, String), Me.DataGridTableStyle.DataGrid.Font, New SolidBrush(Me.DataGridTableStyle.SelectionForeColor), bounds.X + 2, bounds.Y + 2)
40             End If

41         Else
42             g.FillRectangle(backBrush, bounds)
43             If IsDBNull(bVal) Then

44                 g.DrawString(String.Empty, Me.DataGridTableStyle.DataGrid.Font, foreBrush, bounds.X + 2, bounds.Y + 2)
45             Else

46                 g.DrawString(CType(bVal, String), Me.DataGridTableStyle.DataGrid.Font, foreBrush, bounds.X + 2, bounds.Y + 2)
47             End If

48 
49         End If
50     End Sub
51 
52 End Class
53