Visual Basic .NET/DataGridView
Appearance
DataGridView
[edit | edit source]To change the cell formatting based on content, handle the CellFormatting event. Other methods (e.g. setting the RowDefaultCellStyle) can lose the formatting if the user clicks on a column header to sort the grid.
To link other controls to the grid, use the SelectionChanged event. The RowEnter event can give the wrong behaviour, as it only fires when the focus box enters a row. Again, sorting by clicking on a column header can cause unpredictable behaviour.
To retrieve a DataRow from a databound grid, use:
DirectCast(mydatagridview.Rows(i).DataBoundItem, DataRowView).Row