分享
 
 
 

向DataGrid控件中添加ComboBox控件

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在前面看到了很多关于怎样向DataGrid中添加ComboBox控件的方法。使用的方法全部都是在VB6.0中的方法。

我还是要说说在CSND中发贴的朋友。

现在所谓的.NET编程人员,不知道是怎么了呢!只是停留在使用.NET的编程环境中。并没有真正的了解面向对象的.NET编程思想。

我现在就利用继承DataGridColumnStyle完成向DataGrid中添加ComboBox。

希望这样有助于大家了解真正的面向对象编程的思想。不要只是认为利用VB6.0中的某些方法就是.NET高手了。有这种思想的人都是菜鸟 (希望这么说没有得罪太多的朋友:)下面就是实现的代码:我使用的是VB.NET来完成的。

我熟悉C#,但是VB.NET只是大概了解一下。应该比一些人要高一点点吧!:)

见笑了!由于时间关系没有协注释,请见谅!

Public Class DataGridComboColumn

Inherits DataGridColumnStyle

Public WithEvents DGCombo As ComboBox = New ComboBox

Private isEditing As Boolean

Private _strSelectedText As String

Public Sub New()

MyBase.New()

DGCombo.Visible = False

End Sub

Protected Overrides Sub Abort(ByVal rowNum As Integer)

isEditing = False

RemoveHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged

Invalidate()

End Sub

Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean

DGCombo.Bounds = Rectangle.Empty

AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged

If isEditing = False Then

Return True

End If

isEditing = False

Try

DGCombo.Text = DGCombo.Text

Catch ex As Exception

DGCombo.Text = String.Empty

End Try

Try

Dim value As String = _strSelectedText

SetColumnValueAtRow(dataSource, rowNum, value)

Catch ex As Exception

Abort(rowNum)

Return False

End Try

Invalidate()

Return True

End Function

Protected 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)

Dim value As String

Try

value = CType(GetColumnValueAtRow(source, rowNum), String)

Catch ex As Exception

SetColumnValueAtRow(source, rowNum, DGCombo.Text)

End Try

value = CType(GetColumnValueAtRow(source, rowNum), String)

If (cellIsVisible) Then

DGCombo.Bounds = New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)

DGCombo.Text = value

DGCombo.Visible = True

AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged

Else

DGCombo.Text = value

DGCombo.Visible = False

End If

If DGCombo.Visible = False Then

DataGridTableStyle.DataGrid.Invalidate(bounds)

End If

End Sub

Protected Overrides Function GetMinimumHeight() As Integer

End Function

Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer

End Function

Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

End Function

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 Integer)

Paint(g, bounds, source, rowNum, True)

End Sub

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 Integer, ByVal alignToRight As Boolean)

Paint(g, bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight)

End Sub

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 Integer, _

ByVal backBrush As System.Drawing.Brush, _

ByVal foreBrush As System.Drawing.Brush, _

ByVal alignToRight As Boolean)

Dim strDate As String

Dim rect As RectangleF

Try

strDate = DGCombo.Text

strDate = CType(GetColumnValueAtRow(source, rowNum), String)

Catch ex As Exception

SetColumnValueAtRow(source, rowNum, DGCombo.Text)

strDate = CType(GetColumnValueAtRow(source, rowNum), String)

End Try

rect.X = bounds.X

rect.Y = bounds.Y

rect.Height = bounds.Height

rect.Width = bounds.Width

g.FillRectangle(backBrush, rect)

rect.Offset(0, 2)

rect.Height -= 2

g.DrawString(strDate, Me.DataGridTableStyle.DataGrid.Font, foreBrush, rect)

End Sub

Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)

MyBase.SetDataGridInColumn(value)

If Not (DGCombo.Parent Is Nothing) Then

DGCombo.Parent.Controls.Remove(DGCombo)

End If

If Not (value Is Nothing) Then

value.Controls.Add(DGCombo)

End If

End Sub

Private Sub DGCombo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGCombo.SelectedValueChanged

isEditing = True

MyBase.ColumnStartedEditing(DGCombo)

_strSelectedText = DGCombo.Text

If _strSelectedText Is Nothing Then

_strSelectedText = String.Empty

End If

End Sub

End Class

以下是使用方法!

Private Sub frmDataGrid_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Call TEST()

End Sub

Private Sub TEST()

Dim DT As New DataTable("TEST")

DT.Columns.Add(New DataColumn("ID"))

DT.Columns.Add(New DataColumn("COMBO"))

Call AddGridStyle()

Call ADDDATA(DT)

DBGrid.DataSource = DT

End Sub

Private Sub AddGridStyle()

Dim DBGridStyle As DataGridTableStyle

Dim IDColumn As DataGridTextBoxColumn

Dim DCColumn As DataGridComboColumn

DBGridStyle = New DataGridTableStyle

DBGridStyle.MappingName = "TEST"

IDColumn = New DataGridTextBoxColumn

IDColumn.MappingName = "ID"

IDColumn.HeaderText = "ID"

IDColumn.Alignment = HorizontalAlignment.Center

IDColumn.Width = 50

DBGridStyle.GridColumnStyles.Add(IDColumn)

DCColumn = New DataGridComboColumn

Dim i As Integer

For i = 0 To 25

DCColumn.DGCombo.Items.Add((i + 1).ToString("00000"))

Next

DCColumn.DGCombo.DropDownWidth = 120

DCColumn.MappingName = "COMBO"

DCColumn.HeaderText = "COMBO"

DCColumn.Alignment = HorizontalAlignment.Center

DCColumn.Width = 60

DBGridStyle.GridColumnStyles.Add(DCColumn)

DBGrid.TableStyles.Add(DBGridStyle)

End Sub

Private Sub ADDDATA(ByRef DT As DataTable)

Dim DROW As DataRow

Dim intRow As Integer

For intRow = 0 To 9

DROW = DT.NewRow()

DROW.Item("ID") = Format(intRow + 1, "000")

DROW.Item("COMBO") = Format(intRow + 1, "00000")

DT.Rows.Add(DROW)

Next

DT.AcceptChanges()

End Sub

那么随时在DataGrid中所指定为Combo列中单击。Combo就出现了

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有