Vadim
Vadim

Reputation: 83

DataGridView formatting

I have a DGV with columns "code" and "name".

Depends of lenght of a code I want to add tabulation to the "name" cells, to show structure of a data.

Like that in this picture:

https://lh3.ggpht.com/_DoguEKXT64k/S6Hoq7Eu7sI/AAAAAAAABRs/wwjf7TXTLmk/dgv.jpg

How is it better to do? I think there is a better way then just loop for all rows and add spaces in front of names, right?

Upvotes: 1

Views: 456

Answers (1)

Bobby
Bobby

Reputation: 11576

You could hook into the DataGridView.CellFormatting-Event. That will be called for every cell when needed.

Edit: This is a variant of the code Vadim posted in the comments:

Public Overrides Sub DGVCellFormatting(ByVal e As DataGridViewCellFormattingEventArgs)
    If DGVMain.Columns(e.ColumnIndex).Name = "Name" Then
        Dim cellValue As String = DGVMain.Rows(e.RowIndex).Cells("Code").Value.ToString()
        e.Value = cellValue.PadLeft(3 * (cellValue.Length - 3))
    End If
End Sub

Upvotes: 3

Related Questions