DataGridViewColumn
The DataGridView control can have several different types of columns depending on what exactly you want to show.
You can access the columns in a DataGridView by using the Columns collection
dgvDataGridView.Columns
You can access the selected columns by using the SelectedColumns collection
dgvDataGridView.SelectedColumns
When data is not bound you should create an instance of a column type and then add it to the Columns collection.
When you define a column, you have to specify a cell template on which the column is based.
DataGridViewTextBoxColumn
This column type has a cell template defined for a textbox.
By default this column type has its cell template initialised to a new DataGridViewTextBoxCell.
Setting a Default Value
Inherit a class from DataGridViewColumn and override the DefaultNewRowValue property
Properties
Name | |
Width | |
ReadOnly |
DataGridViewComboBoxColumn
Used to display a drop-down list inside a column of cells.
You can populate the drop-down list either manually or by binding it to a data source through the DataSource, DisplayMember and ValueMember properties.
When you add rows to the datagridview make sure this column is assigned a value from the drop-down list.
Single Click on Drop-Down
It is possible to have the drop-down displayed on a single click rather than 2 clicks.
objDataGridView.EditMode = Windows.Forms.DataGridViewEditMode.EditOnEnter
Display Drop-Down when you click on cell
objDataGridView_CellEnter()
If e.ColumnIndex = 2 Then
If Me.objDataGridView(e.ColumnIndex, e.RowIndex).EditType.ToString = _
"System.Windows.Forms.DataGridViewComboBoxEditingControl" Then
System.Windows.Forms.SendKeys.Send("{F4}")
End If
End If
Properties
Name | |
Width | |
DropDownWidth | |
MaxDropDownItems | |
FlatStyle | |
Style | This is set to DropDown to default. Change it to DropDownList to edit as well. |
Events
SelectedIndexChanged | Occurs when the selected index has changed, ie a different item is being selected. |
CellBeginEdit | |
SelectionChangeCommitted | Occurs when the selected item has changed and that change is displayed in the ComboBox. |
DataGridViewComboBoxColumn - Multiple Selection
CellFormatting
SelectionChanged
EditingControlShowing
m_objEditingComboBox = CType(e.Control, Forms.ComboBox)
AddHandler m_objEditingComboBox.SelectedIndexChanged
AddHandler m_objEditingComboBox.DropDown
AddHandler m_objEditingComboBox.DropDownClosed
AddHandler m_objEditingComboBox.KeyDown
AddHandler m_objEditingComboBox.KeyUp
Adding Columns
objdatagridviewcolumn = New System.Windows.Forms.DataGridViewColumn
objdatagridviewcolumn.Name = "Title"
objdatagridviewcolumn.Width = 100
objdatagridviewcolumn.CellTemplate = New System.Windows.Forms.DataGridViewTextBoxCell
dgvSearchResults.Columns.Add(objdatagridviewcolumn)
Hiding Columns
The minimum width for a column is 5 and if you need to hide the column completely you can change the visible property in the Columns dialog box.
Sorting with a Particular Column
By default, users can sort the data in a DataGridView control by clicking the header of a text box column.
You can modify the SortMode property of specific columns to allow users to sort by other column types when it makes sense to do so.
You can also sort the data programmatically by any column, or by multiple columns.
The sort mode for each column is specified through the SortMode property of the column, which can be set to one of the following DataGridViewColumnSortMode enumeration values.
Automatic | Default for text box columns. Unless column headers are used for selection, clicking the column header automatically sorts the DataGridView by this column and displays a glyph indicating the sort order. |
NotSortable | Default for non-text box columns. You can sort this column programmatically; however, it is not intended for sorting, so no space is reserved for the sorting glyph. |
Programmatic | You can sort this column programmatically, and space is reserved for the sorting glyph. |
You might want to change the sort mode for a column that defaults to NotSortable if it contains values that can be meaningfully ordered.
For example, if you have a database column containing numbers that represent item states, you can display these numbers as corresponding icons by binding an image column to the database column. You can then change the numerical cell values into image display values in a handler for the System.Windows.Forms.DataGridView.CellFormatting event. In this case, setting the SortMode property to Automatic will enable your users to sort the column. Automatic sorting will enable your users to group items that have the same state even if the states corresponding to the numbers do not have a natural sequence. Check box columns are another example where automatic sorting is useful for grouping items in the same state.
You can sort a DataGridView programmatically by the values in any column or in multiple columns, regardless of the SortMode settings. Programmatic sorting is useful when you want to provide your own user interface (UI) for sorting or when you want to implement custom sorting. Providing your own sorting UI is useful, for example, when you set the DataGridView selection mode to enable column header selection. In this case, although the column headers cannot be used for sorting, you still want the headers to display the appropriate sorting glyph, so you would set the SortMode property to Programmatic.
Columns set to programmatic sort mode do not automatically display a sorting glyph. For these columns, you must display the glyph yourself by setting the System.Windows.Forms.DataGridViewColumnHeaderCell.SortGlyphDirection property. This is necessary if you want flexibility in custom sorting. For example, if you sort the DataGridView by multiple columns, you might want to display multiple sorting glyphs or no sorting glyph.
Although you can programmatically sort a DataGridView by any column, some columns, such as button columns, might not contain values that can be meaningfully ordered. For these columns, a SortMode property setting of NotSortable indicates that it will never be used for sorting, so there is no need to reserve space in the header for the sorting glyph.
When a DataGridView is sorted, you can determine both the sort column and the sort order by checking the values of the SortedColumn and SortOrder properties. These values are not meaningful after a custom sorting operation. For more information about custom sorting, see the Custom Sorting section later in this topic.
When a DataGridView control containing both bound and unbound columns is sorted, the values in the unbound columns cannot be maintained automatically. To maintain these values, you must implement virtual mode by setting the VirtualMode property to true and handling the CellValueNeeded and CellValuePushed events. For more information, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control. Sorting by unbound columns in bound mode is not supported.
Programmatic Sorting
You can sort a DataGridView programmatically by calling its Sort method.
The Sort(DataGridViewColumn,ListSortDirection) overload of the Sort method takes a DataGridViewColumn and a ListSortDirection enumeration value as parameters. This overload is useful when sorting by columns with values that can be meaningfully ordered, but which you do not want to configure for automatic sorting. When you call this overload and pass in a column with a SortMode property value of System.Windows.Forms.DataGridViewColumnSortMode.Automatic, the SortedColumn and SortOrder properties are set automatically and the appropriate sorting glyph appears in the column header.
When the DataGridView control is bound to an external data source by setting the DataSource property, the Sort(DataGridViewColumn,ListSortDirection) method overload does not work for unbound columns. Additionally, when the VirtualMode property is true, you can call this overload only for bound columns. To determine whether a column is data-bound, check the IsDataBound property value. Sorting unbound columns in bound mode is not supported.
Custom Sorting
You can customize DataGridView by using the Sort(IComparer) overload of the Sort method or by handling the SortCompare event.
The Sort(IComparer) method overload takes an instance of a class that implements the IComparer interface as a parameter. This overload is useful when you want to provide custom sorting; for example, when the values in a column do not have a natural sort order or when the natural sort order is inappropriate. In this case, you cannot use automatic sorting, but you might still want your users to sort by clicking the column headers. You can call this overload in a handler for the ColumnHeaderMouseClick event if you do not use column headers for selection.
The Sort(IComparer) method overload works only when the DataGridView control is not bound to an external data source and the VirtualMode property value is false. To customize sorting for columns bound to an external data source, you must use the sorting operations provided by the data source. In virtual mode, you must provide your own sorting operations for unbound columns.
As an alternative to the Sort(IComparer) method overload, you can provide custom sorting by implementing a handler for the SortCompare event. This event occurs when users click the headers of columns configured for automatic sorting or when you call the Sort(DataGridViewColumn,ListSortDirection) overload of the Sort method. The event occurs for each pair of rows in the control, enabling you to calculate their correct order.
The SortCompare event does not occur when the DataSource property is set or when the VirtualMode property value is true.
Me.dataGridView1.Columns("Priority").SortMode = DataGridViewColumnSortMode.Automatic
Sorting by a column - leave the current row highlighted but do not alter the scroll bar
maybe always leave the totoal frozen at the top ?
AllowSorting
This is a property of the DataGridTableStyle and indicates whether sorting is allowed on the grid table.
It has a default value of TRUE.
objDataGridTableStyle.AllowSorting = True
When the AllowSorting property is True, a triangle appears in each column header indicating the direction of the sort.
The user can click on any column header to sort the grid by that column.
Clicking the column header a second time will automatically sort in the opposite direction.
This property overrides the AllowSorting property of the DataGrid object.
Custom Sorting
You can customise the sorting of your data by using the MouseDown and MouseUp events.
Private mbSortColumn As Boolean
Private Sub dgrDataGrid_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles dgrDataGrid.MouseDown
Dim objDataGridHitTestInfo As DataGrid.HitTestInfo
'Only use left mouse button clicks as right might have shortcut menu
If e.Button = MouseButtons.Left Then
'Perform a hit test to determine where the mousedown event occured.
objDataGridHitTestInfo = dgrDataGrid.HitTest(e.X, e.Y)
If objDataGridHitTestInfo.Type = dgrDataGrid.HitTestType.ColumnHeader Then
mbSortColumn = True
End If
End If
End Sub
Private Sub dgrDataGrid_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles dgrDataGrid.MouseUp
Dim objDataGridHitTestInfo As DataGrid.HitTestInfo
Dim objDataTable As DataTable
Dim objDataView As DataView
Dim scolumnname As String
' Use only left mouse button clicks.
If e.Button = MouseButtons.Left And
mbSortColumn = True Then
mbSortColumn = False 'Reset mouse button state
' Perform a hit test to determine where the mousedown event occured.
objDataGridHitTestInfo = dataGrid.HitTest(e.X, e.Y)
' If the mousedown event occured on a column header,
' then perform the sorting operation.
If objDataGridHitTestInfo.Type = dgrDataGrid.HitTestType.ColumnHeader Then
objDataTable = dgrDataGrid.DataSource
objDataView = dgrDataTable.DefaultView
scolumnname = dataTable.Columns(hitTest.Column).ColumnName
' If the sort property of the DataView is already the current column name, sort that in column descending order.
' Otherwise, sort on the column name.
If dataView.Sort = scolumnname Then
dataView.Sort = scolumnname + " DESC"
Else
dataView.Sort = scolumnname
End If
End If
End If
End Sub
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext