473,788 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Color the entire row based on the value in one column in WinForms's Datagrid

Hello,
Can please tell me how to do it ? This should be simple, because I'd think
it's used a lot.. but in reality is not that simple and there is at least 50
topics about it in newsgroups, but the problem is that people see the
question and just write about overriding OnPaint or pointing to the
syncfusion site without reading the question.. it's coloring the entire row,
not just one cell.. I can't believe it, but there is so many answers, but
not a single useful one. When I figure that out, I'll post it (if somebody
doesn't send the answer here).

Please, someone.. actually even if somebody provided a solution for this, I
need something more..
I need to colour the entire row based on specific underlying DataRow field
value.

for example:
myDataGrid.Data Source = myDataTable;

DataRows in DataTable are like this:
r["name"] = ...
r["lastname"] = ...
r["marked"] = true/false
..
..
..

but only first two are shown in datagrid, like:

DataGridColored TextBoxColumn cs = new DataGridColored TextBoxColumn() ;
cs.MappingName = "name";
cs.HeaderText = "Name";
myDataGrid.Grid ColumnStyles.Ad d(cs);

....
so.. I want to color red all the rows in DataGrid with "marked" set to true
in its DataSource's DataRows.

Please help.

your help would be most appreciated!

David
Nov 16 '05 #1
4 1917
Don't know if this is the best solution, but it's one I use, and best of all,
it works...
You need to define define the 'ItemDataBound' for your datagrid, see sample
below:

private void myDataGrid_Item DataBound(objec t sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
DataRowView rv = (DataRowView)e. Item.DataItem;
if (rv.Row.ItemArr ay[2]) //check if 'marked'
{
e.Item.Cells[0].BackColor = Color.Red;
e.Item.Cells[0].ForeColor = Color.White;
e.Item.Cells[1].BackColor = Color.Red;
e.Item.Cells[1].ForeColor = Color.White;
e.Item.Cells[2].BackColor = Color.Red;
e.Item.Cells[2].ForeColor = Color.White;
}
}
}
"David Krmpotic" wrote:
Hello,
Can please tell me how to do it ? This should be simple, because I'd think
it's used a lot.. but in reality is not that simple and there is at least 50
topics about it in newsgroups, but the problem is that people see the
question and just write about overriding OnPaint or pointing to the
syncfusion site without reading the question.. it's coloring the entire row,
not just one cell.. I can't believe it, but there is so many answers, but
not a single useful one. When I figure that out, I'll post it (if somebody
doesn't send the answer here).

Please, someone.. actually even if somebody provided a solution for this, I
need something more..
I need to colour the entire row based on specific underlying DataRow field
value.

for example:
myDataGrid.Data Source = myDataTable;

DataRows in DataTable are like this:
r["name"] = ...
r["lastname"] = ...
r["marked"] = true/false
..
..
..

but only first two are shown in datagrid, like:

DataGridColored TextBoxColumn cs = new DataGridColored TextBoxColumn() ;
cs.MappingName = "name";
cs.HeaderText = "Name";
myDataGrid.Grid ColumnStyles.Ad d(cs);

....
so.. I want to color red all the rows in DataGrid with "marked" set to true
in its DataSource's DataRows.

Please help.

your help would be most appreciated!

David

Nov 16 '05 #2
Thank you, but I think your solution is good only for WebForms and it won't
work on WinForms :(
"Paul Horstink" <Paul Ho******@discus sions.microsoft .com> wrote in message
news:B9******** *************** ***********@mic rosoft.com...
Don't know if this is the best solution, but it's one I use, and best of all, it works...
You need to define define the 'ItemDataBound' for your datagrid, see sample below:

private void myDataGrid_Item DataBound(objec t sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
DataRowView rv = (DataRowView)e. Item.DataItem;
if (rv.Row.ItemArr ay[2]) //check if 'marked'
{
e.Item.Cells[0].BackColor = Color.Red;
e.Item.Cells[0].ForeColor = Color.White;
e.Item.Cells[1].BackColor = Color.Red;
e.Item.Cells[1].ForeColor = Color.White;
e.Item.Cells[2].BackColor = Color.Red;
e.Item.Cells[2].ForeColor = Color.White;
}
}
}
"David Krmpotic" wrote:
Hello,
Can please tell me how to do it ? This should be simple, because I'd think it's used a lot.. but in reality is not that simple and there is at least 50 topics about it in newsgroups, but the problem is that people see the
question and just write about overriding OnPaint or pointing to the
syncfusion site without reading the question.. it's coloring the entire row, not just one cell.. I can't believe it, but there is so many answers, but not a single useful one. When I figure that out, I'll post it (if somebody doesn't send the answer here).

Please, someone.. actually even if somebody provided a solution for this, I need something more..
I need to colour the entire row based on specific underlying DataRow field value.

for example:
myDataGrid.Data Source = myDataTable;

DataRows in DataTable are like this:
r["name"] = ...
r["lastname"] = ...
r["marked"] = true/false
..
..
..

but only first two are shown in datagrid, like:

DataGridColored TextBoxColumn cs = new DataGridColored TextBoxColumn() ;
cs.MappingName = "name";
cs.HeaderText = "Name";
myDataGrid.Grid ColumnStyles.Ad d(cs);

....
so.. I want to color red all the rows in DataGrid with "marked" set to true in its DataSource's DataRows.

Please help.

your help would be most appreciated!

David

Nov 16 '05 #3
Hi David,

http://msdn.microsoft.com/library/de...stdatagrid.asp

I hope this helps, take a look at "Figure 7 The DataGrid with Colored
Rows"

regards

Santiago Corredoira
www.syltek.com
Nov 16 '05 #4
Thank you!
I too stumbled upon this one just a second ago and it seems that it solves
the general problem..
I still have to dwelve into the code, because it's not that trivial.. I just
hope I'll be able to do the same not depending just on one column value, but
on underlying datarow. thank you.
"Santi" <no************ *********@gmail .com> wrote in message
news:cv******** **@nsnmrro2-gest.nuria.tele fonica-data.net...
Hi David,

http://msdn.microsoft.com/library/de...stdatagrid.asp
I hope this helps, take a look at "Figure 7 The DataGrid with Colored
Rows"

regards

Santiago Corredoira
www.syltek.com

Nov 16 '05 #5

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

Similar topics

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
1420
by: Harold | last post by:
Hi all, having a little difficulty binding up a dropdownlist control in a datagrid column. I know I must be missing something Here are 2 methods. One is the DataGrid's ItemDataBound event to catch the grid population event and the other is to populate the dropdownlist I'll start with the method that binds the dropdownlist. As mentioned it is object based and also works fine everywhere but in a template column. I have put in some comments...
0
1349
by: DaveR | last post by:
I have a webform with a two-column datagrid based on an Arraylist. The Arraylist draws the data for the two columns from two different tables in an SQL database. The data is displayed in datagrid as expected but when user clicks the Edit link, the cells do NOT turn into editable text boxes. I actually need only the first column to be editable but any ideas on getting the whole row editable would help at first. Apologies for the amount of...
3
2650
by: Starbuck | last post by:
Hi Using VB.Net and Datagrid. I have figured out how to set the color of a column depending on a value in the column, but how do I color the entire row? Thanks in advance
3
1092
by: brix_zx2 | last post by:
I'm writing a program in VB .NET 2k3, I have a datagrid bound to a database (Access 2002). Does anyone have info or know where I can go to change the color of a row based on the value in one column? I have found stuff on alternating colors and all that good stuff but nothing that I need. Thanks
1
2125
by: Mike P | last post by:
Is it possible when populating a datagrid to populate a dropdownlist column based upon the value populated to another row in the datagrid? (i.e. I have a drop down which I want to populate differently for each row with user names based upon a UserRegionID which is also a column in the row). Since you don't actually know the value of the UserRegionID until the data is already bound to the datagrid, how do you do this?
2
2129
by: Billy | last post by:
Change DataGrid EditControl On Data Value Hi, I have a datagrid, and on editing, I want to change the control in the third colunm based on the value of the first column. The value in the first column can only be either "Text" or "Image", which is selcted in a dropdown list.
1
1508
by: Steve | last post by:
C# WinForms I am trying to find a good solution for changing the color of a row in a datagrid, at runtime, based on the contents of a certain cell of the row, or a related value in the database. I cant believe it is as hard as I am finding. This is surely a common problem. All the solutions I have found so far though, seem very specific, and that if I want this in various datagrids through my application I will have to have a load...
2
3443
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3 datagrida. I would like to set it so that if value of column 3 in datagrid 1 is < 100 set to red, if = 100 set to green if value of column 3 in datagrid 2 is < 90 set to red, if >= 90 set to green if value of column 3 in datagrid 3is < 80 set to red,...
0
10364
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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
7517
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
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
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.