Share via


VB.Net ExtendedDataGridView control


Introduction

The standard .Net DataGridView is a fairly versatile customizable control, that allows viewing and editing tables of data which can be either bound or unbound. The ExtendedDataGridView adds some extra functionality to the standard .Net DataGridView making it easier to use in the case of the extra Events, and the printing and exporting as CSV, further enhance the control. 

In a simple application that binds data to the DataGridView, it's slightly more difficult to use the ExtendedDataGridView, but not impossible by any means. The issue is, if you allow the DataGridView to AutogenerateColumns, it will use the standard DataGridViewColumns. Fortunately changing column types in code is a simple task, or you can set AutogenerateColumns to False and bind your columns manually.

↑ Return to Top


Overview

This is an extended DataGridView control that adds three public methods, a ContextMenuStrip, and two custom Events to a standard DataGridView control. 

The three methods are:

  • Print()
  • PreviewPrint()
  • SaveasCSV()

with the purposes being fairly self-explanatory.

The ContextMenuStrip has MenuItems for invoking those three methods and a Reveal MenuItem which hosts a UserControl used for revealing hidden columns. 

The (DataGridView) control uses custom columns which provide an extra property to each extended version of the standard DataGridView columns, allowing selection of two custom HeaderCells for the extended DataGridViewCheckBoxColumn, which are:

  • checkAllHeaderCell
  • checkHideColumnHeaderCell

along with:

Again the purposes of the HeaderCells are self-explanatory.

All of the other extended DataGridViewColumns don't have the CheckAll option.

The Printer class called by the Print method is optimized to print any combination of columns and rows and also prints visual elements, such as ComboBoxes, CheckBoxes, Images, Links, and Cell backcolors.

Both the Print and the PreviewPrint methods use a custom PrintDialog, which allows selection of a printer, page ranges (FromPage, ToPage), page orientation, number of copies and collating pages in multiple copy print runs.

The SaveasCSV option allows exporting the DataGridView contents as a CSV file.

The custom Events provided by the extended DataGridView are:

cellCheckedChanged - which exposes these properties in the DataGridViewCheckBoxCell:

  • Public columnIndex As Integer
  • Public rowIndex As Integer
  • Public newValue As Boolean

cellSelectedIndexChanged - which exposes these properties in the DataGridViewComboBoxCell:

  • Public columnIndex As Integer
  • Public rowIndex As Integer
  • Public selectedIndex As Integer
  • Public selectedItem As Object
  • Public selectedValue As Object
  • Public text As String

Figure 1.

Figure 2.

Figure 3.

Figure 4.

Figure 5.

Figure 6.

↑ Return to Top


The Extended Columns - An Example

Imports System.ComponentModel
 
Public Class  altDataGridViewCheckBoxColumn
    Inherits DataGridViewCheckBoxColumn
 
    Private _HeaderStyle As enumerations.style2
    <Category("Design"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Property  HeaderStyle() As  enumerations.style2
        Get
            Return _HeaderStyle
        End Get
        Set(ByVal value As enumerations.style2)
            _HeaderStyle = value
            Select Case  _HeaderStyle
                Case style2.Standard
                    MyBase.HeaderCell = New  DataGridViewColumnHeaderCell
                Case style2.CheckAll
                    MyBase.HeaderCell = New  checkAllHeaderCell
                Case style2.HideColumn
                    MyBase.HeaderCell = New  checkHideColumnHeaderCell
            End Select
        End Set
    End Property
 
    Public Overrides  Function clone() As Object
        Dim copyColumn As altDataGridViewCheckBoxColumn = DirectCast(MyBase.Clone, altDataGridViewCheckBoxColumn)
        copyColumn._HeaderStyle = Me.HeaderStyle
        Return copyColumn
    End Function
 
End Class

↑ Return to Top


Conclusion

Overall, the ExtendedDataGridView provides a simpler experience for the developer. Two issues that are queried regularly are how to capture CheckedChanged events and how to get more information about the ComboBox selection in a DataGridView. The ExtendedDataGridView simplifies both of these issues by providing two custom Events.

Another two issues regularly asked are how to add a CheckBox to a HeaderCell. This is another addition included in the ExtendedDataGridView. DataGridViewCheckBoxColumns HeaderCells allow CheckAll CheckBoxes or HideColumn CheckBoxes, and all of the other extended column types allow just HideColumn CheckBoxes in the HeaderCell.

Also, Printing is a major hurdle for a developer utilizing a DataGridView. In the  ExtendedDataGridView control, that is a built-in feature. To print your DataGridView, you just need to call it's Print() method. Similarly, you can preview your printing by calling the PreviewPrint() method.

Finally, you can also export to CSV by calling the SaveasCSV() method.

These additional features enable faster application development without leaving out valuable functionality...

↑ Return to Top


Other Resources

(The download is available in C# and VB)

Download from MSDN Samples Gallery

↑ Return to Top