Multiple Column Formats in Windows Forms DataGrid

KDNGrid Class

KDNGrid Class enhances the Windows Forms Datagrid with CheckBox, ComboBox, NumericUpDown, DateTimePicker, and MultiLine TextBox columns. The class includes all of the previously listed DataGridColumnStyles and installs with the source code and a demonstration form and Microsoft Access database.

The code of the class is too extensive to explain here, but the source code is included with the demonstration project. The demo loads a DataTable from both memory and database. You can download the VB.NET code by clicking here. If you are developing in C#, for now, you can create a DLL with the VB.NET code and call it from the C# code. We plan to upload the C# version of the code soon.

Figure 1 shows the VB.NET code for creating the TableStyle, creating and adding the ColumnStyles to the TableStyle, and setting the grid style and binding the data to the grid.

Figure 1 - VB.NET Code To Create Grid Columns.

Private Sub LoadGrid()
      'This method creates a DataTable to use for the Grid

      Dim dt As New DataTable("MyFirstTable")
      BuildDataTable(dt)

      'Create a TableStyle to contain the columnstyles for the grid.
      '(An alternate method for this is at the end of this method)
      Dim ts As DataGridTableStyle = CGrid.GetTableStyle(dt)

      'Create the ColumnStyles and add each one to the TableStyle.
      'Normally, we would build all the ColumnStyles then add 
      'them all to the tablestyle, but because the Multiline 
      'style may or may not be created, each ColumnStyle is 
      'added individually.

      'CheckBox Column
      Dim cs1 As New CGridCheckBoxStyle("Column1", 60, _
                                        HorizontalAlignment.Center, _
                                        False, "Select", _
                                        String.Empty, "N", "Y", _
                                        False, String.Empty)
      CGrid.AddColumn(ts, cs1)

      'TextBox Column
      Dim cs2 As New CGridTextBoxStyle("Column2", 80, _
                                        HorizontalAlignment.Left, _
                                        False, "Editable", _
                                        String.Empty, String.Empty)
      CGrid.AddColumn(ts, cs2)

      'DateTimePicker Column
      Dim cs3 As New CGridDateTimePickerStyle("Column3", 220, _
                                              False, "MyDate", _
                                              DateTimePickerFormat.Custom, _
                                              "F", "MM/dd/yyyy hh:mm:ss tt")
      CGrid.AddColumn(ts, cs3)

      'TextBox Column - ReadOnly
      Dim cs4 As New CGridTextBoxStyle("Column4", 100, _
                                       HorizontalAlignment.Left, True, _
                                       "Non Editable", "", "")
      CGrid.AddColumn(ts, cs4)

      'ComboBox Column
      Dim Items() As String = {"Yes", "No", "Maybe", "Depends"}
      Dim cs5 As New CGridComboBoxStyle("Column5", 80, _
                                        HorizontalAlignment.Left, _
                                        "Your Pick", "(null)", _
                                        Items, ComboBoxStyle.DropDownList)
      CGrid.AddColumn(ts, cs5)

      'NumericUpDown Column
      Dim cs6 As New CGridNumericUpDownStyle("Column6", 60, "Count", 0, 100, _
                                             0, 1, LeftRightAlignment.Right, _
                                             0, "#,##0")
      CGrid.AddColumn(ts, cs6)

      'Multiline TextBox Column
      If Me.chkMultiLineText.Checked Then
         Dim cs7 As New CGridMultiLineTextBoxStyle("Column7", 200, _
                                                   HorizontalAlignment.Left, _
                                                   False, "MultiLine Column", _
                                                   String.Empty)
         CGrid.AddColumn(ts, cs7)
      Else
         Dim cs7 As New CGridTextBoxStyle("column7", 150, _
                                          HorizontalAlignment.Left, _
                                          False, "ML Text", _
                                          String.Empty, String.Empty)
         CGrid.AddColumn(ts, cs7)
      End If


      'Set an AlternatingBackColor - just for looks
      ts.AlternatingBackColor = Color.LightGoldenrodYellow

      'Set the TableStyle for the Grid
      CGrid.SetGridStyle(Me.DataGrid1, dt, ts)

      'Uncomment this to prevent the user 
      'from adding rows to the grid
      'CGrid.DisableAddNew(DataGrid1, Me)

      'Turn off the title bar for the grid
      Me.DataGrid1.CaptionVisible = False

      'ALTERNATE METHOD - Create a strongly typed 
      '                   collection and add the 
      '                   created ColumnStyles
      'Dim col As New DataGridColumnStyleCollection()
      'col.Add(cs1)
      'col.Add(cs2)
      'col.Add(cs3)
      'col.Add(cs4)
      'col.Add(cs5)
      'col.Add(cs6)
      'col.Add(cs7)
      ''Build the Grid
      'cg.SetGridStyle(Me.DataGrid1, dt, col)

   End Sub

Figure 2 shows the code for determining when a CheckBox column is clicked. We use the Datagrid Mouse_Up event to trap when a check box column has been clicked. This code will determine if a check box column was clicked and if so, will call a method to set the DataSource to the status of the grid column.

Figure 2 - CheckBox Column Event Handling.

Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
      'This code will handle changing the value of the CheckBox column in 
      'column1 on a single click, rather than having to click once to activate,
      'then again to change the value.  It also will select the entire row
      'when column1 is clicked.

      Dim ClickedRowIndex As Integer
      Dim bChecked As Boolean
      Dim ClickedColumnName As String
      Dim result As Object = Nothing

      'Determine what was clicked
      ClickedRowIndex = CGrid.GetClickedCellAndRow(CType(DataGrid1.DataSource, DataTable), Me.DataGrid1, ClickedColumnName, result, False)

      If ClickedRowIndex > -1 AndAlso ClickedColumnName.ToLower = "column1" Then
         'Column1 was clicked - Toggle the value and select the row
         ClickedRowIndex = CGrid.SelectCheckBoxRow(CType(DataGrid1.DataSource, DataTable), Me.DataGrid1, e, "Column1", bChecked, 0, True)
         result = bChecked
      End If

      If ClickedRowIndex > -1 Then
         'Update the UI
         Me.lblCellClicked.Text = ClickedColumnName
         Me.lblRowClicked.Text = ClickedRowIndex.ToString
         If Not result Is Nothing Then
            Me.lblValue.Text = result.ToString
         End If
      End If

   End Sub

KDNGridDemo.zip (244 KB)