Datagridview cell format to show substring of column text in vb.net
I have a column item code, inside my database, that I have bound to a datagrid view. The item code comes in this "ABC" format, I only want to show part of the "B" code, I bound this column to the gridview and now I want it to show the substring. I tried defaultcellstyle.format but don't know how to get the substring for it.
+2
source to share
2 answers
Is it possible to add a new property to a bound object like ItemCodePart that returns the middle of your code item and then binds that property to a column instead of a code item? This would be the easiest way.
Another option is to handle the CellFormatting event in the DataGridView and set e.Value to the part of the item's code that you want to show:
Private Sub myDataGridView_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles myDataGridView.CellFormatting
If e.ColumnIndex = MyItemPartColumn.Index Then
Dim currentValue As String = CStr(myDataGridView.Item(e.ColumnIndex, e.RowIndex).Value)
Dim parts As String() = currentValue.Split(New Char() {"-"c})
e.Value = parts(1)
End If
End Sub
+2
source to share