Anirudh Goel
Anirudh Goel

Reputation: 4711

format cell of datagridview to show substring of column text in vb.net

i have a column item-code, inside my database which i have bound to a datagrid view. The item-code comes in this format "A-B-C", i wish only to show the "B" part of the code, i have bound this column to the gridview and now wish to make it show the substring. I tried defaultcellstyle.format but don't know how to get a substring for it.

Upvotes: 2

Views: 3383

Answers (2)

Meta-Knight
Meta-Knight

Reputation: 17845

Is it a possibility to add a new property to your bound object, something like ItemCodePart, which returns the middle part of your item-code, then bind this property to the column instead of item-code? That would be the simplest way.

Another option is to handle the CellFormatting event of the DataGridView and set e.Value to the part of item-code 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

Upvotes: 2

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

RowDataBound event - You can edit the text of that field.

Upvotes: 0

Related Questions