473,769 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11537
Create a DataGridColumnS tyle for that column by inheriting from the
DataGridTextBox Column 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*******@disc ussions.microso ft.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:Dat aGridTextBoxCol umn
{
/..
OTHER OVER RIDDEN METHODS
.../

protected override void Paint(Graphics g,Rectangle
Bounds,Currency Manager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Sourc e,RowNum,Brushe s.LightGoldenro dYellow,ForeB
rush,AlignToRig ht);
}
}

Set Datagrid Style:-

PropertyDescrip torCollection pcol
= this.BindingCon text[ds, "Lines"].GetItemPropert ies();

GridDelColumn = new ColumnStyle
(pcol["Lines"]);
GridDelColumn.M appingName
= "NewQty";
GridDelColumn.H eaderText = "xxxx";
GridDelColumn.W idth = 75;
GridDelColumn.R eadOnly = false;
GridDelColumn.A lignment =
HorizontalAlign ment.Right;
DGStyle.GridCol umnStyles.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.
PropertyDescrip torCollection pcol
= this.BindingCon text[ds, "Lines"].GetItemPropert ies();

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*******@disc ussions.microso ft.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:Dat aGridTextBoxCol umn
{
/..
OTHER OVER RIDDEN METHODS
../

protected override void Paint(Graphics g,Rectangle
Bounds,Currency Manager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Sourc e,RowNum,Brushe s.LightGoldenro dYellow,ForeB
rush,AlignToRig ht);
}
}

Set Datagrid Style:-

PropertyDescrip torCollection pcol
= this.BindingCon text[ds, "Lines"].GetItemPropert ies();

GridDelColumn = new ColumnStyle
(pcol["Lines"]);
GridDelColumn.M appingName
= "NewQty";
GridDelColumn.H eaderText = "xxxx";
GridDelColumn.W idth = 75;
GridDelColumn.R eadOnly = false;
GridDelColumn.A lignment =
HorizontalAlign ment.Right;
DGStyle.GridCol umnStyles.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 : DataGridTextBox Column
{
public ColumnStyle()
{}
protected override void Paint(Graphics g,Rectangle
Bounds,Currency Manager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Sourc e,RowNum,Brushe s.LightGoldenro dYellow,ForeB
rush,AlignToRig ht);
}
}

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 DataGridTextBox Column. 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*******@disc ussions.microso ft.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 : DataGridTextBox Column
{
public ColumnStyle()
{}
protected override void Paint(Graphics g,Rectangle
Bounds,Currency Manager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
base.Paint
(g,Bounds,Sourc e,RowNum,Brushe s.LightGoldenro dYellow,ForeB
rush,AlignToRig ht);
}
}

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 : DataGridTextBox Column
{
Color m_BackGroundCol or;

public DGColumn(Color BackGroundColor )
{
m_BackGroundCol or = BackGroundColor ;
this.TextBox.Ba ckColor = m_BackGroundCol or;
}
protected override void Paint(Graphics g,Rectangle
Bounds,Currency Manager Source,int RowNum, Brush
BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
Brush backBrush = new SolidBrush(m_Ba ckGroundColor);
base.Paint
(g,Bounds,Sourc e,RowNum,backBr ush,ForeBrush,A lignToRight);
}
}
Nov 16 '05 #7

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

Similar topics

1
1471
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 i have made this non-editable and therefore i do not want the graey colour that appears when the user clicks on a cell in the first row. As only my second row is editable, i only want a click colour for this coloumn
4
12822
by: Dave Petersen | last post by:
How do I set the color of a particular column in my C# winforms datagrid? Thanks, Dave
1
4095
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 selection on gray background)... i want red background for example... -- thank you serge
0
2663
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 CompanyName, ContactName, and ContactTitle columns from the Customers table in the Northwind database. Whenever the value of ContactTitle equals "Owner" we will change the font color to red. We can do this by using an ItemTemplate where we have...
0
1197
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, if the column name has two words, if they were displayed one on top of the other, they now are displayed on one line and the text will not wrap. This behavior does not appear in Netscape or Mozilla or Opera, just IE. The problem is killing me...
4
1867
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, the entire row (all columns) are displayed in a background color. I have figured out how to do it cell by cell, just not the entier row at once. How would i do this? thanks!
0
2624
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 header row - and they conflict. The sortable column apparently uses the generic style for <a> elements and the rest of the header uses "headerRow". Can anyone tell me how to get consistent styles for each column in my datagrid header?
7
2766
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
1887
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 recessed it in a <Div></Divto allow scrolling of just the data, not the page. What I need to do is place a DataList (also in a <Div></Divto allow scrolling of just the data) to the right of the Datagrid to show 40 Employee Detail fields (listed top to...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.