To add a ComboBox control to the DataGrid control

Source: Internet
Author: User
Tags empty integer
ComboBox Control |datagrid|datagrid Control

There are a lot of ways to add a ComboBox control to the DataGrid before you see it. The methods used are all methods in VB6.0.

I still want to talk about the friend who posts in the csnd.

Now the so-called. NET programmer, do not know how it is! Just stay in use. NET in the programming environment. And there's no real understanding of object-oriented. NET programming idea.

I'm now using the inheritance DataGridColumnStyle to finish adding ComboBox to the DataGrid.

Hopefully this will help you understand the idea of real object-oriented programming. Don't just think that some of the methods used in VB6.0 are. NET Pros. People who have this kind of thinking are all amateurs (hopefully this is not too much to offend friends:) The following is the code of implementation: I'm using vb.net to do it.

I'm familiar with C #, but vb.net just about. Should be a little taller than some people! :)

I laughed! Because the time relationship does not have the association annotation, please forgive me!

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, Byva L bounds as System.Drawing.Rectangle, ByVal [readOnly] as Boolean, ByVal Instanttext as String, ByVal cellisvisible as Boo Lean
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, Byva L 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, Byva L 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 are 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.selectedval Uechanged
isediting = True
Mybase.columnstartedediting (Dgcombo)
_strselectedtext = Dgcombo.text
If _strselectedtext is nothing Then
_strselectedtext = String.Empty
End If
End Sub
End Class

Here's how to use it!

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

Then click in the combo column at any time as specified in the DataGrid. Combo's on the show.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.