datagrid row color
-
hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color
-
hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color
Use the oncellpaint event (I think) and query the object to find what row/column is being painted and set the background accordingly. Note that this fires for each cell that is on display, not the entire data source
Never underestimate the power of human stupidity RAH
-
hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color
-
did you try this:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.BackgroundColor = Color.Red;
if (i % 2 == 0)
{
dataGridView1.BackgroundColor = Color.Blue;
}
}Qendro
Surely that just sets the background colour for the whole DataGridView, not for individual rows?
-
hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color
You can set the cell style to use on alternate Rows within the DataGridView. This article shows how to do it: http://www.dotnetcurry.com/ShowArticle.aspx?ID=132&AspxAutoDetectCookieSupport=1[^] Tip 12.
-
Surely that just sets the background colour for the whole DataGridView, not for individual rows?
i tried this and it worked:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
e.CellStyle.BackColor = Color.Red;
if (i % 2 == 0)
{
e.CellStyle.BackColor = Color.Blue;
}
}
}Qendro
-
i tried this and it worked:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
e.CellStyle.BackColor = Color.Red;
if (i % 2 == 0)
{
e.CellStyle.BackColor = Color.Blue;
}
}
}Qendro
It might work (if you replace Columns with Rows), but it doesn't look very efficient. Also, you don't need to do this anyway since DataGridView has a built in property that does it for you (AlternatingRowsDefaultCellStyle).
-
i tried this and it worked:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
e.CellStyle.BackColor = Color.Red;
if (i % 2 == 0)
{
e.CellStyle.BackColor = Color.Blue;
}
}
}Qendro
hi guys thanks for the reply i tried qendro's codes but i have a small problem with it also..it works but not as i wished..it works like that : if i have one or 3 rows in datagridview then datagridview's backcolor becomes blue but when i add another row then it turns to white.. u see it is a bit different than i wished.. i want like that first row will be blue and second white and third blue again and fourth will be white again and so on..any other suggestions you have thanks all for the replies
-
You can set the cell style to use on alternate Rows within the DataGridView. This article shows how to do it: http://www.dotnetcurry.com/ShowArticle.aspx?ID=132&AspxAutoDetectCookieSupport=1[^] Tip 12.