473,382 Members | 1,375 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,382 software developers and data experts.

DataGrid Background Color

233 100+
I have a windows forms application, currently with a datagrid, that I would like to dynamically change the background color on a row by row basis based off of values in child rows. Parent rows are customer based, and the child rows are order based. If order > x, then set background color to red. If order>y, then set background color to yellow. I would prefer to do all this when the datagrid is loaded, but I cannot find a way to get this to work. Any help would be greatly appreciated.

Keep in mind that this is in regards to a windows forms app, and not asp or .aspx.
Mar 25 '09 #1
9 3149
Plater
7,872 Expert 4TB
I have not found a nice way to do this myself, but I have gotten it to work by using the CellFormatting event.
Unfortunatly it gets called for EVERY cell (as that cell becomes visible on the screen) and not on a row by row basis.

Assume that I have a DataGridView called: dgvNeedMapping

Expand|Select|Wrap|Line Numbers
  1. private void dgvNeedMapping_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  2. {
  3.    int rowidx = e.RowIndex;
  4.    DataGridViewRow dgr = dgvNeedMapping.Rows[rowidx];
  5.    Color startcolor = e.CellStyle.BackColor;//previous background color
  6. //now you can determine what color to use and apply it.
  7. //for me I did this:
  8. //dgr.DefaultCellStyle.BackColor=Color.FromName(dgr.Cells["ColorName"].Value.ToString());
  9. }
  10.  
Mar 25 '09 #2
mcfly1204
233 100+
Thank you for the response. However, I was hoping that I would be able to do this using a datagrid as opposed to the datagridview. The datagrid was easier to display the parent/child information in one grid. I want to check a value in the child row, and then change the background color in the parent row if the specified criteria is met.
Mar 25 '09 #3
mcfly1204
233 100+
I broke down and am now using two dgv's, one showing customers, and the other shows the customer's invoices when selected. This works fine, but next question is how can I incorporate changing the background color of the parent row based on a column in a child row? I do not know enough about bound dgv's and how parent/child interaction works.
Mar 26 '09 #4
Plater
7,872 Expert 4TB
If you have switched to DataGridViews, you can edit the code i showed above to change the backgrounds
Mar 26 '09 #5
mcfly1204
233 100+
@Plater
Your original code was for datagridview, which does not translate to datagrids. What I was asking about is whether or not it is possible to change the parent row background color based on values in a corresponding child row.

My thoughts are that this could become complex given there are x number of rows per parent row. My solution was to create a view to include the needed field from the child row in the parent row. This way your original code will work as needed for the parent dgv.
Mar 26 '09 #6
Plater
7,872 Expert 4TB
Right I saw that, and was stumped as you at changing DataGrid's colors.
BUT then you came back and said you were using dgvs, which I took to mean DataGridViews, which then falls under the realm of my posted code.
Are you still using DataGrid objects, just no longer nested?
Mar 26 '09 #7
mcfly1204
233 100+
@Plater
I was initially using datagrid's due to the ease in displaying nested rows. I switched to datagridviews after your initial post and after reading through a lot of forums posts with little promise. Currently, I am working with two datagridviews bound by a relation.
Mar 26 '09 #8
Plater
7,872 Expert 4TB
Ok then, so you can use my code to change the background color of the datagridviews you are using then?
Mar 27 '09 #9
mcfly1204
233 100+
Yes, the code you originally posted works for changing the background color of a datagridview based on a particular column within that datagridview.
Mar 27 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chico | last post by:
Hi, I have an ASP page that was built using MS Visual InterDev. The original programmer used the MS Visual InterDev DataGrid control to render his recordset. What I need to do is to change one...
1
by: Tayo | last post by:
What is best way of having a consistent datagrid format on multiple pages? Any ideas welcome Tayo
9
by: tshad | last post by:
Is there a way to use your own image in place of the automatic one that ASP uses when doing editing in your DataGrid pages? We already have a style of button we are using and would like to be...
2
by: karunakar | last post by:
Hi All In datagrid OnMouseOver iam showing some color Onmouseout also working fine . Here my problem is when ever click the pariculat row in DataGrid iam showing red color that time i am not...
0
by: Jennifer | last post by:
I had this beautiful datagrid on an ASP page (VB.Net as the code behind). When I clicked on a Select Button, the row was selected and the background color changed the way it was supposed to. The...
0
by: Wasco | last post by:
Hi all, I would like using an extern css file for defining datagrid style: <asp:datagrid id="DataGrid1" runat="server" GridLines="Horizontal" CellPadding="3" CssClass="gridDetail">...
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...
1
by: Brock | last post by:
Thanks in advance... (you can see a screenshot of what my form looks like currently at http://www.juggernautical.com/DataGrid.jpg - the Datalist is super-imposed in 'design view' but the DataGrid...
4
by: Peted | last post by:
Hi I need to iterate through a winforms visible datagrid, in a legacy application, to update it a little bit to change the background colours of the grid. This seems to be more dificult than...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.