Change datagrid column background color

B

Barney Nicholls

I have a datagrid and want to change the background color
of the end column to denote that it is the only column
that is not read only.

I still want this column to be editable.

I've found examples that let you change the background
color of the cell but it but the cell no longer appears
to be editable.

Can anybody help me >

Barney
 
D

Dmitriy Lapshin [C# / .NET MVP]

Create a DataGridColumnStyle for that column by inheriting from the
DataGridTextBoxColumn and overriding its Paint method. Within the overridden
Paint, call the base.Paint and give it the pre-created brush for the
background instead of the one you've received in the arguments to the
overridden Paint.
 
B

Barney Nicholls

I've done as you suggested and the color is as i wish and
the contents appear fine but the cell appears to be read
only.

I need to be able to edit this value.

my code is as below if this is any help:

public class ColumnStyle:DataGridTextBoxColumn
{
/..
OTHER OVER RIDDEN METHODS
.../

protected override void Paint(Graphics g,Rectangle
Bounds,CurrencyManager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Source,RowNum,Brushes.LightGoldenrodYellow,ForeB
rush,AlignToRight);
}
}

Set Datagrid Style:-

PropertyDescriptorCollection pcol
= this.BindingContext[ds, "Lines"].GetItemProperties();

GridDelColumn = new ColumnStyle
(pcol["Lines"]);
GridDelColumn.MappingName
= "NewQty";
GridDelColumn.HeaderText = "xxxx";
GridDelColumn.Width = 75;
GridDelColumn.ReadOnly = false;
GridDelColumn.Alignment =
HorizontalAlignment.Right;
DGStyle.GridColumnStyles.Add
(GridDelColumn);

Any ideas would be appreciated
 
D

Dmitriy Lapshin [C# / .NET MVP]

Sounds strange, given the Paint method is the only one you've overridden in
the derived column style.
Could you please elaborate on "cell appears read-only"? What happens if you
click on the cell? It refuses to switch to the edit mode? If so, the first
place to look at is the logic in the overridden Edit method.

I am also not sure what does code snippet below do. I've never played with
property descriptors when I worked on custom column styles so I cannot tell
whether these lines can cause the problem.
PropertyDescriptorCollection pcol
= this.BindingContext[ds, "Lines"].GetItemProperties();

GridDelColumn = new ColumnStyle
(pcol["Lines"]);

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Barney Nicholls said:
I've done as you suggested and the color is as i wish and
the contents appear fine but the cell appears to be read
only.

I need to be able to edit this value.

my code is as below if this is any help:

public class ColumnStyle:DataGridTextBoxColumn
{
/..
OTHER OVER RIDDEN METHODS
../

protected override void Paint(Graphics g,Rectangle
Bounds,CurrencyManager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Source,RowNum,Brushes.LightGoldenrodYellow,ForeB
rush,AlignToRight);
}
}

Set Datagrid Style:-

PropertyDescriptorCollection pcol
= this.BindingContext[ds, "Lines"].GetItemProperties();

GridDelColumn = new ColumnStyle
(pcol["Lines"]);
GridDelColumn.MappingName
= "NewQty";
GridDelColumn.HeaderText = "xxxx";
GridDelColumn.Width = 75;
GridDelColumn.ReadOnly = false;
GridDelColumn.Alignment =
HorizontalAlignment.Right;
DGStyle.GridColumnStyles.Add
(GridDelColumn);

Any ideas would be appreciated
 
B

Barney Nicholls

Hello Dmitriy

Thankyou for your help so far with this. I was
unnecessarily overriding some events so i have reduced my
overrides to this:

public class ColumnStyle : DataGridTextBoxColumn
{
public ColumnStyle()
{}
protected override void Paint(Graphics g,Rectangle
Bounds,CurrencyManager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Source,RowNum,Brushes.LightGoldenrodYellow,ForeB
rush,AlignToRight);
}
}

This works fine and i can now edit the coloured cell.
Being picky though when the cell is in edit the back
ground color is changed to white. how do i override this
behaviour.

Thanks again

Barney
 
D

Dmitriy Lapshin [C# / .NET MVP]

The column style hosts the TextBox control within the cell when it is being
edited. This instance of TextBox should be accessible through a protected
(or even public?) property on the DataGridTextBoxColumn. You can try to
alter the textbox's BackColor property value to achieve the desired effect.
 
B

Barney Nicholls

Thank you very much for your help Dmitriy. What I ended
up with was this that works a treat:-

public class DGColumn : DataGridTextBoxColumn
{
Color m_BackGroundColor;

public DGColumn(Color BackGroundColor)
{
m_BackGroundColor = BackGroundColor;
this.TextBox.BackColor = m_BackGroundColor;
}
protected override void Paint(Graphics g,Rectangle
Bounds,CurrencyManager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
Brush backBrush = new SolidBrush(m_BackGroundColor);
base.Paint
(g,Bounds,Source,RowNum,backBrush,ForeBrush,AlignToRight);
}
}
 

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