views:

31

answers:

1

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:

http://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?

+2  A: 

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
Bobby
Do you mean adding spaces in CellFormatting, right?
Vadim
@Vadim: Yes, that was my idea.
Bobby
Thanks. This code works.Public Overrides Sub DGVCellFormatting(ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)If DGVMain.Columns(e.ColumnIndex).Name = "Name" Then e.Value = Space(3 * (DGVMain.Rows(e.RowIndex).Cells("Code").Value.ToString.Length - 3)) + e.Value.ToString
Vadim