Advertisement

01.18.2008 at 06:30AM PST, ID: 23093338
[x]
Attachment Details

How do I change cell color in DataGrid Web Form ?

Asked by gregajesih in C# Programming Language, Miscellaneous Programming

Tags: ,

Hello.
Goal: change datagrid cell forecolor/backcolor depending on cell value.

After some browsing I found plenty answers that simply don't fit class structure that VS2003 gives to a C# code. DataGrid is instantiated over VS 2003 IDE, graphically. Datagrid uses TableStyles and DataGridColumnStyles defined over IDE to give it some decent shaping.

I bind datagrid to a SQL table by this:
..
                  da=new SqlDataAdapter("select * from CTRX_CS",conn);
                  ds=new DataSet();
                              
                  da.Fill(ds);
                  ds.Tables[0].TableName="CTRX_CS";
                  dataGrid1.DataMember = "CTRX_CS";
                  dataGrid1.DataSource = ds;
...

BUT, I can't find any:

- DataGrid cell reference that would include .Foreground. There is one connected to datagrid object
  itself, but is ment 1 for entire datagrid, is it ?

- a friend told me I can use Paint event to alter currentcell. But If I reference for example
  DataGrid1[r][c] there is no .Foreground property. There's only ToString() and a few other properties that don't seem to fit into any color property.

So what to do ?

Thanks.
Grega




Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
...
			da=new SqlDataAdapter("select * from CTRX_CS",conn);
			ds=new DataSet();
					
			da.Fill(ds);
			ds.Tables[0].TableName="CTRX_CS";
			dataGrid1.DataMember = "CTRX_CS";
			dataGrid1.DataSource = ds;
...
 
private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
  int r=dataGrid1.CurrentRowIndex;
  int c=dataGrid1.CurrentCell.ColumnNumber;
 
  // only 1st row is interesting for this
  if (dataGrid1.CurrentCell.ColumnNumber==0) 
  {
       switch( ds.Tables[0].Rows[dataGrid1.CurrentRowIndex]["TIP"].ToString() ) 
       {
	case "SHOP":
	// now what ? I want current cell forecolor red..
         break;
	case "BAR":
         // color cell blue
	break;
	case "TEST":
         // color cell green
	break;
								
        }
  }
}
[+][-]01.18.2008 at 07:35AM PST, ID: 20691057

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.28.2008 at 07:45AM PST, ID: 20759984

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.11.2008 at 11:33PM PDT, ID: 21544948

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Miscellaneous Programming
Tags: C# .net VS2003, IE
Sign Up Now!
Solution Provided By: gregajesih
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628