Call This Routine, (AddDataGridStyle()), Just after the Datagrid1.DataSource = objDs.tables(“Table1”)

 

Cut and Paste Friendly Sub…

Cut and Paste Friendly Class…

 

    Private Sub AddDataGridStyle()

 

        ' Grid HouseKeeping **************************************************************************

        Dim MyGridStyle As DataGridTableStyle = New DataGridTableStyle()                            '*

        MyGridStyle.MappingName = "MyDataSetTable"                                                  '*

        MyGridStyle.AlternatingBackColor = System.Drawing.Color.Gold                                '*

        MyGridStyle.BackColor = System.Drawing.Color.Blue                                           '*

        MyGridStyle.ForeColor = System.Drawing.Color.Red                                            '*

        MyGridStyle.PreferredRowHeight = 40

        '*********************************************************************************************

 

 

        '*** Here Is A Sample DropDownList Grid Item *************************************************

        '                                                                                           '*

        'This Class Needs To Be Added To The Project...                                             '*

        Dim vColCombo As New MyClsDataGridComboBoxColumn()                                           '*

        vColCombo.MappingName = "Age"                                                                '*

        vColCombo.HeaderText = "Age"                                                                 '*

        vColCombo.Width = 150                                                                        '*

        '                                                                                           '*

        '*** Add Some Items....                                                                      *

        vColCombo.MyCombo.Items.Clear()                                                              '*

        vColCombo.MyCombo.Items.Add("1")                                                             '*

        vColCombo.MyCombo.Items.Add("2")                                                             '*

        '                                                                                           '*

        vColCombo.MyCombo.DropDownStyle = ComboBoxStyle.DropDown                                     '*

        MyGridStyle.GridColumnStyles.Add(vColCombo)                                                  '*

        '*********************************************************************************************

 

 

        '*** Here Is A Sample Boolean Grid Item **************************************************

        'Yes or No                                                                              '*

        Dim vBoolColumn As DataGridBoolColumn = New DataGridBoolColumn()                         '*

        vBoolColumn.MappingName = "YesNo"                                                        '*

        vBoolColumn.HeaderText = "Yes or No"                                                     '*

        vBoolColumn.Width = 50                                                                   '*

        MyGridStyle.GridColumnStyles.Add(vBoolColumn)                                            '*

        '*****************************************************************************************

 

        '*** Here Is A Sample Plain Old Text Item ************************************************

        'Name                                                                                    *

        Dim vNameColumnStyle As DataGridTextBoxColumn = New DataGridTextBoxColumn()              '*

        vNameColumnStyle.MappingName = "Name"                                                    '*

        vNameColumnStyle.HeaderText = "Name"                                                     '*

        vNameColumnStyle.Width = 50                                                              '*

        vNameColumnStyle.NullText = "No Name Given"                                              '*

        MyGridStyle.GridColumnStyles.Add(vNameColumnStyle)                                       '*

        '*****************************************************************************************

 

        DataGrid1.TableStyles.Add(MyGridStyle)

 

    End Sub

 

 

ADD THESE 2 CLASSES

'These Classes are used by the AddDataGridStyle Routine...

Public Class MyClsDataGridComboBoxColumn

 

    Inherits DataGridTextBoxColumn

 

    Public MyCombo As MyClsDataGridComboBox

    Private m_isEditing As Boolean

 

    Public Sub New()

        MyBase.New()

 

        MyCombo = New MyClsDataGridComboBox()

        m_isEditing = False

 

        AddHandler MyCombo.Leave, New EventHandler(AddressOf LeaveComboBox)

        AddHandler MyCombo.SelectionChangeCommitted, New EventHandler(AddressOf OnSelectionChangeCommitted)

 

    End Sub

 

    Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

        MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)

 

        MyCombo.Parent = Me.TextBox.Parent

        MyCombo.Location = Me.TextBox.Location

        MyCombo.Size = New Size(Me.TextBox.Size.Width, MyCombo.Size.Height)

        MyCombo.Text = Me.TextBox.Text

        Me.TextBox.Visible = False

        MyCombo.Visible = True

        MyCombo.BringToFront()

        MyCombo.Focus()

 

    End Sub

 

    Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)

        MyCombo.Hide()

    End Sub

 

    Protected Overloads Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean

 

        If m_isEditing Then

            m_isEditing = False

            SetColumnValueAtRow(dataSource, rowNum, MyCombo.Text)

        End If

        Return True

 

    End Function

    Private Sub OnSelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)

 

        m_isEditing = True

        MyBase.ColumnStartedEditing(sender)

 

    End Sub

End Class

 

Public Class MyClsDataGridComboBox

    Inherits ComboBox

 

    Private WM_KEYUP As Integer = &H101

 

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = WM_KEYUP Then

            Return

        End If

        MyBase.WndProc(m)

    End Sub

 

End Class