Modify background of datagrid cell.

J

Jorge

Hello

I am trying to change the backgroup color of windows form
datagrid. I found a sample class at the following URL
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnwinforms/html/wnf_CustDataGrid.asp
I get a compilation error sub 'Paint' cannot be
declared 'Overrides' because it does not override a sub
in a base class.
<code>
Public Class FormattableTextBoxColumn
Inherits DataGridTextBoxColumn

Public Event SetCellFormat As FormatCellEventHandler

Protected Overloads Overrides Sub Paint()

End Sub 'Paint
End Class
</code>

Does anyone know how to correct the compilation error or
has another class that does the same? Thanks


Kind Regards
Jorge Cavalheiro
 
C

Cor Ligthert

Jorge,

I do not know if you want to change the cell or the complete background, I
sand today an almost the same sample I once made to Woody, I changed it a
little, have a look at it, probably it is not what you need, however maybe
it is.

\\\
Private WithEvents dtbCol1 As DataGridTextBox
Private ToolTip1 As New ToolTip
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
Dim dt As New DataTable("test")
ds.Tables.Add(dt)
dt.Columns.Add("1")
For i As Integer = 0 To 10
Dim dr As DataRow = dt.NewRow
dr(0) = i
dt.Rows.Add(dr)
Next
DataGrid1.ReadOnly = True
Dim ts As New DataGridTableStyle
ts.MappingName = ds.Tables(0).TableName
Dim column As New DataGridTextBoxColumn
ts.GridColumnStyles.Add(column)
DataGrid1.TableStyles.Add(ts)
column = DirectCast(ts.GridColumnStyles(0), DataGridTextBoxColumn)
dtbCol1 = DirectCast(column.TextBox, DataGridTextBox)
column.MappingName = ds.Tables(0).Columns(0).ColumnName
column.HeaderText = "Woody"
column.Width = 30
Dim dv As New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
End Sub
Private Sub dtbCol1_ToolTip(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles dtbCol1.MouseEnter
DirectCast(sender, DataGridTextBox).BackColor = Color.Red
End Sub
///
 
G

Gerry O'Brien [MVP]

If you take a look in the Object Browser, you will find the
DataGridTextBoxColumn under the System.Windows. Forms heirarchy.

When you find the class, you will notice that it has three Paint methods.
All three Paint methods are Protected Overrides which means that they are
overriding from another superclass.

Each of these methods are overloaded in the DataGridTextBoxColumn Class.
However, they are not marked as Overridable which means you must implement
one of them as is. You are not providing any parameters to you call for
Paint in this sample code.

Take a look at the method signatures to see what is required for each
method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top