943,714 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2104
  • C# RSS
Dec 8th, 2008
0

changing cell color datagridvew c#

Expand Post »
hi
i'm using a datagrid view control which get's his datasource from a dataset control.

my code is:

c# Syntax (Toggle Plain Text)
  1. dataGrid = new DataGridView();
  2. dataGrid.DataSource = dataSet.Tables["Vertexes"];
  3. this.Controls.Add(dataGrid);

the datagridview in shown fine.
the problem is that when i try to cahnge cells color like this:

c# Syntax (Toggle Plain Text)
  1. for (int x = 0; x < dataGrid.Rows.Count; x++)
  2. {
  3. for (int y = 0; y < dataGrid.Rows[x].Cells.Count; y++)
  4. {
  5. if (x == y)
  6. {
  7. dataGrid.Rows[x].Cells[y].Style.BackColor =
  8. System.Drawing.Color.Red;
  9. }
  10. }
  11. }

it doest eners to the 'for' loop. i can see in debug that the datagridvew rows and columns controls are empty.
Reputation Points: 20
Solved Threads: 0
Junior Poster
emilio is offline Offline
162 posts
since Nov 2007
Dec 8th, 2008
0

Re: changing cell color datagridvew c#

Depends I guess a little on when that code runs (which you havent shown) or, why you dont just use the draw method - for which search this forum ther are a number of examples
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Dec 9th, 2008
0

Re: changing cell color datagridvew c#

hi
this s my code working fine. i think r u creating dynamic datagridview . insead of controlname ur using datagrid may be. so chk it
Quote ...
private void Form1_Load(object sender, EventArgs e)
{

dataGridView1.Rows.Add(5);
dataGridView1.CurrentCell.Selected = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)//dataGridView1.Columns.Count; j++)
{
// dataGridView1.Rows[i].Cells[j].Value = i + 5;
if (i == j)
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Aqua;
}
}
}
Reputation Points: 10
Solved Threads: 23
Junior Poster
Renukavani is offline Offline
123 posts
since Jul 2008
Dec 9th, 2008
0

Re: changing cell color datagridvew c#

Hi,

dataGrid.DataBind() statement is missing.

dataGrid = new DataGridView();
this.Controls.Add(dataGrid);

dataGrid.DataSource = dataSet.Tables["Vertexes"];
dataGrid.DataBind();
Reputation Points: 12
Solved Threads: 4
Light Poster
reena12 is offline Offline
32 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Return value failed in Invoke
Next Thread in C# Forum Timeline: Need help Marshaling in C#...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC