Override DataGridTextBoxColumn class to set cell color 
Author Message
 Override DataGridTextBoxColumn class to set cell color

Russel,
Is this VS.NET Beta 2 or VS.NET RTM?

On the surface your code looks correct...

Rather than removing and adding the new column style, what happens when you
build  a new DataGridTableStyle completely from scratch, adding each
DataGridColumnStyle yourself? (no removes!)

I will try to look at your code closer later this evening...

Hope this helps
Jay


Quote:
> I'd really appreciate if you can tell me what went wrong in my
> implementation, after coded the way as described in Jay Harlow's post, I
> still cannot change the backcolor and forecolor for the customized column.

> To follow Jay's test instruction, as in below:

> >1. Create a new class that derives from DataGridTextBoxColumn
> >2. Add a BackColor property
> >3. Add a ForeColor property
> >4. Override all three Paint Overrides.
> >4a. The one with 4 parameters, I simply call the one with 5 parameters
> >(MyClass.Paint) sending False for the fifth parameter.
> >4b. The one with 5 parameters, I call the one with 7 parameters
> >(MyBase.Paint) creating a new SolidBrush for Me.ForeColor and
Me.BackColor.
> >4c. The one with 7 parameters, I call the one with 7 parameters
> >(MyBase.Paint) creating a new SolidBrush for Me.ForeColor and
Me.BackColor.

> >On #4c I have a variation that calls MyBase.PaintText, which I believe is
> >the helper function used by MyBase.Paint. I get the same results...

> I derive a class from DataGridTextBoxColumn, called

DataGridMyTextBoxColumn

- Show quoted text -

Quote:
> (attached).

> Then I have done this (see code below) to add the column with the 1st
column
> name as mappingname, remove the 1st column from Grid, add the newly
created
> column of type DataGridMyTextBoxColumn to the end of the Grid.

> Dim gcs As GridColumnStylesCollection =
> Me.DataGridTableStyle1.GridColumnStyles

> Dim myTextBoxColumn As New DataGridMyTextBoxColumn()

> With myTextBoxColumn

> .MappingName = "ProductID"

> End With

> gcs.RemoveAt(0)

> gcs.Add(CType(myTextBoxColumn, DataGridColumnStyle))

> Me.DataGridTableStyle1.DataGrid.TableStyles.Clear()

> Me.DataGridTableStyle1.DataGrid.TableStyles.Add(Me.DataGridTableStyle1)

> Then in the following code, I tried to set the backcolor and forecolor
(new
> properties added in the drived class), however, has no effect.

> Dim gcs As GridColumnStylesCollection =

DataGridTableStyle1.GridColumnStyles

- Show quoted text -

Quote:

> Dim i As Integer

> Dim myColumn As DataGridMyTextBoxColumn

> For i = 0 To gcs.Count - 1

> If gcs.Item(i).MappingName = "ProductID" Then

> myColumn = CType(gcs.Item(i), DataGridMyTextBoxColumn)

> myColumn.BackColor = Color.Azure

> myColumn.ForeColor = Color.Brown

> End If

> Next



Wed, 21 Jul 2004 08:10:01 GMT  
 Override DataGridTextBoxColumn class to set cell color
Glad to see your reply, Jay.

I've found the problem.  In the drived
DataGridMyTextBoxColumn class, I forgot to 'override'
besides 'overloads' the 3rd 'paint' (the one with 7
parameters).

In fact, I found only the 3rd 'paint' gets called all the
time.

What I ended up was to replace Windows generated code part
in the test form to make it 'new' a
DataGridMyTextBoxcolumn instead of the default
DataGridTextBoxColumn, then everything worked as expected.

Still have not figured out a proper way to replace all the
default DataGridTextBoxColumn.  I am thinking to make a
drived DataGrid control class from the built in DataGrid
control, then somewhere I can override the default
DataGridColumnStyle class it uses (normally are either
DataGridTextBoxColumn and DataGridBooleanColumn), so it
always use my own DataGridColumnStyle drived classes
instead.  But I couldn't find any document to get a clue
to do this.  (it's fairly easy to do somehting like this
in other OOP environment, Java, C++, etc).

In fact, if I can create such a customized DataGrid class,
I intend to collect other DataGridColumnStyle classes to
put into it, like Combobox, image, etc those others have
made possible with .Net DataGrid control.  Would this be a
great open source project? (or MS could provide a sample
for this to demonstrate the flexibility of .Net's
DataGrid).

Quote:
>-----Original Message-----
>Russel,
>Is this VS.NET Beta 2 or VS.NET RTM?

>On the surface your code looks correct...

>Rather than removing and adding the new column style,

what happens when you
Quote:
>build  a new DataGridTableStyle completely from scratch,
adding each
>DataGridColumnStyle yourself? (no removes!)

>I will try to look at your code closer later this
evening...

>Hope this helps
>Jay


message

>> I'd really appreciate if you can tell me what went
wrong in my
>> implementation, after coded the way as described in Jay
Harlow's post, I
>> still cannot change the backcolor and forecolor for the
customized column.

>> To follow Jay's test instruction, as in below:

>> >1. Create a new class that derives from

DataGridTextBoxColumn

- Show quoted text -

Quote:
>> >2. Add a BackColor property
>> >3. Add a ForeColor property
>> >4. Override all three Paint Overrides.
>> >4a. The one with 4 parameters, I simply call the one
with 5 parameters
>> >(MyClass.Paint) sending False for the fifth parameter.
>> >4b. The one with 5 parameters, I call the one with 7
parameters
>> >(MyBase.Paint) creating a new SolidBrush for
Me.ForeColor and
>Me.BackColor.
>> >4c. The one with 7 parameters, I call the one with 7
parameters
>> >(MyBase.Paint) creating a new SolidBrush for
Me.ForeColor and
>Me.BackColor.

>> >On #4c I have a variation that calls MyBase.PaintText,
which I believe is
>> >the helper function used by MyBase.Paint. I get the
same results...

>> I derive a class from DataGridTextBoxColumn, called
>DataGridMyTextBoxColumn
>> (attached).

>> Then I have done this (see code below) to add the
column with the 1st
>column
>> name as mappingname, remove the 1st column from Grid,
add the newly
>created
>> column of type DataGridMyTextBoxColumn to the end of
the Grid.

>> Dim gcs As GridColumnStylesCollection =
>> Me.DataGridTableStyle1.GridColumnStyles

>> Dim myTextBoxColumn As New DataGridMyTextBoxColumn()

>> With myTextBoxColumn

>> .MappingName = "ProductID"

>> End With

>> gcs.RemoveAt(0)

>> gcs.Add(CType(myTextBoxColumn, DataGridColumnStyle))

>> Me.DataGridTableStyle1.DataGrid.TableStyles.Clear()

>> Me.DataGridTableStyle1.DataGrid.TableStyles.Add

(Me.DataGridTableStyle1)
Quote:

>> Then in the following code, I tried to set the

backcolor and forecolor

- Show quoted text -

Quote:
>(new
>> properties added in the drived class), however, has no
effect.

>> Dim gcs As GridColumnStylesCollection =
>DataGridTableStyle1.GridColumnStyles

>> Dim i As Integer

>> Dim myColumn As DataGridMyTextBoxColumn

>> For i = 0 To gcs.Count - 1

>> If gcs.Item(i).MappingName = "ProductID" Then

>> myColumn = CType(gcs.Item(i), DataGridMyTextBoxColumn)

>> myColumn.BackColor = Color.Azure

>> myColumn.ForeColor = Color.Brown

>> End If

>> Next

>.



Wed, 21 Jul 2004 09:35:51 GMT  
 Override DataGridTextBoxColumn class to set cell color
Forget to answer your question, it's RTM.

Quote:
>-----Original Message-----
>Russel,
>Is this VS.NET Beta 2 or VS.NET RTM?

>On the surface your code looks correct...

>Rather than removing and adding the new column style,

what happens when you
Quote:
>build  a new DataGridTableStyle completely from scratch,
adding each
>DataGridColumnStyle yourself? (no removes!)

>I will try to look at your code closer later this
evening...

>Hope this helps
>Jay


message

>> I'd really appreciate if you can tell me what went
wrong in my
>> implementation, after coded the way as described in Jay
Harlow's post, I
>> still cannot change the backcolor and forecolor for the
customized column.

>> To follow Jay's test instruction, as in below:

>> >1. Create a new class that derives from

DataGridTextBoxColumn

- Show quoted text -

Quote:
>> >2. Add a BackColor property
>> >3. Add a ForeColor property
>> >4. Override all three Paint Overrides.
>> >4a. The one with 4 parameters, I simply call the one
with 5 parameters
>> >(MyClass.Paint) sending False for the fifth parameter.
>> >4b. The one with 5 parameters, I call the one with 7
parameters
>> >(MyBase.Paint) creating a new SolidBrush for
Me.ForeColor and
>Me.BackColor.
>> >4c. The one with 7 parameters, I call the one with 7
parameters
>> >(MyBase.Paint) creating a new SolidBrush for
Me.ForeColor and
>Me.BackColor.

>> >On #4c I have a variation that calls MyBase.PaintText,
which I believe is
>> >the helper function used by MyBase.Paint. I get the
same results...

>> I derive a class from DataGridTextBoxColumn, called
>DataGridMyTextBoxColumn
>> (attached).

>> Then I have done this (see code below) to add the
column with the 1st
>column
>> name as mappingname, remove the 1st column from Grid,
add the newly
>created
>> column of type DataGridMyTextBoxColumn to the end of
the Grid.

>> Dim gcs As GridColumnStylesCollection =
>> Me.DataGridTableStyle1.GridColumnStyles

>> Dim myTextBoxColumn As New DataGridMyTextBoxColumn()

>> With myTextBoxColumn

>> .MappingName = "ProductID"

>> End With

>> gcs.RemoveAt(0)

>> gcs.Add(CType(myTextBoxColumn, DataGridColumnStyle))

>> Me.DataGridTableStyle1.DataGrid.TableStyles.Clear()

>> Me.DataGridTableStyle1.DataGrid.TableStyles.Add

(Me.DataGridTableStyle1)
Quote:

>> Then in the following code, I tried to set the

backcolor and forecolor

- Show quoted text -

Quote:
>(new
>> properties added in the drived class), however, has no
effect.

>> Dim gcs As GridColumnStylesCollection =
>DataGridTableStyle1.GridColumnStyles

>> Dim i As Integer

>> Dim myColumn As DataGridMyTextBoxColumn

>> For i = 0 To gcs.Count - 1

>> If gcs.Item(i).MappingName = "ProductID" Then

>> myColumn = CType(gcs.Item(i), DataGridMyTextBoxColumn)

>> myColumn.BackColor = Color.Azure

>> myColumn.ForeColor = Color.Brown

>> End If

>> Next

>.



Wed, 21 Jul 2004 09:37:06 GMT  
 Override DataGridTextBoxColumn class to set cell color
Hi Russel,

I would be great if somebody could share his experience of working with
Forms DataGrid. Are you finally able to control the colour of invidual
cells? Can you post here or send me a sample of code?
Thanks,

Vladimir L'vov


Quote:
> Glad to see your reply, Jay.

> I've found the problem.  In the drived
> DataGridMyTextBoxColumn class, I forgot to 'override'
> besides 'overloads' the 3rd 'paint' (the one with 7
> parameters).

> In fact, I found only the 3rd 'paint' gets called all the
> time.

> What I ended up was to replace Windows generated code part
> in the test form to make it 'new' a
> DataGridMyTextBoxcolumn instead of the default
> DataGridTextBoxColumn, then everything worked as expected.

> Still have not figured out a proper way to replace all the
> default DataGridTextBoxColumn.  I am thinking to make a
> drived DataGrid control class from the built in DataGrid
> control, then somewhere I can override the default
> DataGridColumnStyle class it uses (normally are either
> DataGridTextBoxColumn and DataGridBooleanColumn), so it
> always use my own DataGridColumnStyle drived classes
> instead.  But I couldn't find any document to get a clue
> to do this.  (it's fairly easy to do somehting like this
> in other OOP environment, Java, C++, etc).

> In fact, if I can create such a customized DataGrid class,
> I intend to collect other DataGridColumnStyle classes to
> put into it, like Combobox, image, etc those others have
> made possible with .Net DataGrid control.  Would this be a
> great open source project? (or MS could provide a sample
> for this to demonstrate the flexibility of .Net's
> DataGrid).

> >-----Original Message-----
> >Russel,
> >Is this VS.NET Beta 2 or VS.NET RTM?

> >On the surface your code looks correct...

> >Rather than removing and adding the new column style,
> what happens when you
> >build  a new DataGridTableStyle completely from scratch,
> adding each
> >DataGridColumnStyle yourself? (no removes!)

> >I will try to look at your code closer later this
> evening...

> >Hope this helps
> >Jay


> message

> >> I'd really appreciate if you can tell me what went
> wrong in my
> >> implementation, after coded the way as described in Jay
> Harlow's post, I
> >> still cannot change the backcolor and forecolor for the
> customized column.

> >> To follow Jay's test instruction, as in below:

> >> >1. Create a new class that derives from
> DataGridTextBoxColumn
> >> >2. Add a BackColor property
> >> >3. Add a ForeColor property
> >> >4. Override all three Paint Overrides.
> >> >4a. The one with 4 parameters, I simply call the one
> with 5 parameters
> >> >(MyClass.Paint) sending False for the fifth parameter.
> >> >4b. The one with 5 parameters, I call the one with 7
> parameters
> >> >(MyBase.Paint) creating a new SolidBrush for
> Me.ForeColor and
> >Me.BackColor.
> >> >4c. The one with 7 parameters, I call the one with 7
> parameters
> >> >(MyBase.Paint) creating a new SolidBrush for
> Me.ForeColor and
> >Me.BackColor.

> >> >On #4c I have a variation that calls MyBase.PaintText,
> which I believe is
> >> >the helper function used by MyBase.Paint. I get the
> same results...

> >> I derive a class from DataGridTextBoxColumn, called
> >DataGridMyTextBoxColumn
> >> (attached).

> >> Then I have done this (see code below) to add the
> column with the 1st
> >column
> >> name as mappingname, remove the 1st column from Grid,
> add the newly
> >created
> >> column of type DataGridMyTextBoxColumn to the end of
> the Grid.

> >> Dim gcs As GridColumnStylesCollection =
> >> Me.DataGridTableStyle1.GridColumnStyles

> >> Dim myTextBoxColumn As New DataGridMyTextBoxColumn()

> >> With myTextBoxColumn

> >> .MappingName = "ProductID"

> >> End With

> >> gcs.RemoveAt(0)

> >> gcs.Add(CType(myTextBoxColumn, DataGridColumnStyle))

> >> Me.DataGridTableStyle1.DataGrid.TableStyles.Clear()

> >> Me.DataGridTableStyle1.DataGrid.TableStyles.Add
> (Me.DataGridTableStyle1)

> >> Then in the following code, I tried to set the
> backcolor and forecolor
> >(new
> >> properties added in the drived class), however, has no
> effect.

> >> Dim gcs As GridColumnStylesCollection =
> >DataGridTableStyle1.GridColumnStyles

> >> Dim i As Integer

> >> Dim myColumn As DataGridMyTextBoxColumn

> >> For i = 0 To gcs.Count - 1

> >> If gcs.Item(i).MappingName = "ProductID" Then

> >> myColumn = CType(gcs.Item(i), DataGridMyTextBoxColumn)

> >> myColumn.BackColor = Color.Azure

> >> myColumn.ForeColor = Color.Brown

> >> End If

> >> Next

> >.



Thu, 22 Jul 2004 02:08:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Setting font and color attributes in a cell

2. Setting Excel Cell Colors from Visual Basic

3. Help for setting cell color in DBGRID

4. How to set each Cell of Grid different Color

5. Setting Color for Grid Cells

6. How to set color to cell in dbgrid ?

7. set color to 1-cell in DBgrid

8. Listview: set cell color

9. MSFlex Grid: Setting the color and font of cells

10. setting the cell color with mshflexgrid

11. setting all foreground and bkground colors to use window's current color setting

12. How to change cell color or font color when data changed

 

 
Powered by phpBB® Forum Software