向DataGrid控制項中添加ComboBox控制項

來源:互聯網
上載者:User
combobox控制項|datagrid|datagrid控制項  

在前面看到了很多關於怎樣向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就出現了



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。