473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Change datagrid column background color

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
Nov 16 '05 #1
6 11522
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.

--
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" <an*******@discussions.microsoft.com> wrote in message
news:16****************************@phx.gbl...
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


Nov 16 '05 #2
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.LightGoldenrodYell ow,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
Nov 16 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:18****************************@phx.gbl... 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.LightGoldenrodYell ow,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


Nov 16 '05 #4
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.LightGoldenrodYell ow,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
Nov 16 '05 #5
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.

--
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" <an*******@discussions.microsoft.com> wrote in message
news:1b****************************@phx.gbl...
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.LightGoldenrodYell ow,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


Nov 16 '05 #6
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,AlignT oRight);
}
}
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JD | last post by:
How do i get rid of the 1st column that appears in a datagrid? I do not want this? Also how do i change the colour of the background of a cell when the user selects it. i.e for the first coloumn...
4
by: Dave Petersen | last post by:
How do I set the color of a particular column in my C# winforms datagrid? Thanks, Dave
1
by: Serdge Kooleman | last post by:
Hi! i have a windows app, DataGrid on it... when i clicked at cell, it's appear small edit box in it. how to change foregraund and background color of this text? (by default it is blue...
0
by: Amber | last post by:
There are times when you will need to highlight or otherwise modify the contents of a particular DataGrid row-column value based upon the value in the column. In this example we will select the...
0
by: Stanley Alex | last post by:
I am having a problem with the way my DataGrid displays when a column header is clicked on to sort records. When the column name is clicked, a white background appears behind the column name. Also,...
4
by: Brian Henry | last post by:
I have a data grid with data, which each row will tie to a link to open that row in a seperate window of data for it, the data grid has multiple columns. I want it so when a mouse rolls over a row,...
0
by: cedoucette | last post by:
I just wrote code to support sortable columns in a datagrid. It seems to work fine; but, it doesn't look right. The problem is that I have a generic style for links and a different style for the...
7
by: PJ | last post by:
How can you set the background color of individual columns in a datagrid to be different to others? The table styles only allow you to do this at the grid level for all columns.
1
by: Brock | last post by:
First note that I am using Framework 1.1. I have an .aspx page that is displaying a list of employees, but only the Employee Number, First Name, Last Name, and Title. It is working great. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.