当前位置:编程学习 > asp >>

向DataGrid控件中添加ComboBox控件

 

在前面看到了很多关于怎样向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, _
                          &nbs

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,