开发者

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 th开发者_JS百科at in this picture:

DataGridView formatting

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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜