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

my code is:

dataGrid = new DataGridView();
dataGrid.DataSource = dataSet.Tables["Vertexes"];
this.Controls.Add(dataGrid);

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

for (int x = 0; x < dataGrid.Rows.Count; x++)
            {
                for (int y = 0; y < dataGrid.Rows[x].Cells.Count; y++)
                {
                    if (x == y)
                    {
                        dataGrid.Rows[x].Cells[y].Style.BackColor =
                        System.Drawing.Color.Red;
                    }
                }
            }

it doest eners to the 'for' loop. i can see in debug that the datagridvew rows and columns controls are empty.

Recommended Answers

All 3 Replies

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

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

private void Form1_Load(object sender, EventArgs e)
        {

            dataGridView1.Rows.Add(5);
            dataGridView1.CurrentCell.Selected = false;
            for (int i = 0; i < [COLOR="Red"]dataGridView1[/COLOR].Rows.Count; i++)
            {
                for (int j = 0; j < [COLOR="Red"]dataGridView1[/COLOR].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;
                }
            }
        }

Hi,

dataGrid.DataBind() statement is missing.

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

dataGrid.DataSource = dataSet.Tables["Vertexes"];
dataGrid.DataBind();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.