Overriding DataGridViewTextBoxCell and CellPainting

1.3k views Asked by At

I inherited DataGridViewTextBoxCell because I need to add some custom property to it.

At run-time after creating the DataGridView instance and bind the data I do the following:

For k As Integer = 0 To grid.Columns.Count - 1
                grid.Columns(k).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
                If k > 0 Then grid.Columns(k).CellTemplate = New CustomCell()
Next

with the attempt to add to each column different from the first the CustomCell.

The problem is that when I attach to the CellPainting event of the grid and do

If e.ColumnIndex < 0 OrElse e.RowIndex <= 0 Then Return

Dim grid As DataGridView = TryCast(sender, DataGridView)
If grid Is Nothing Then Return
If grid.Columns.Count < e.ColumnIndex OrElse grid.Rows.Count < e.RowIndex Then Return

Dim cell As CustomCell = TryCast(grid(e.ColumnIndex, e.RowIndex), CustomCell)
If cell Is Nothing Then Return

the cell type is never of type CustomCell but it remains DataGridViewTextBoxCell.

Do you have any idea how can I resolve this issue?

I am working with VS2008 and Framework 3.5

1

There are 1 answers

1
Wade73 On

I believe you want to add a custom column. Here is an article I believe may be helpful - Code Magazine.