Color the entire row based on the value in one column in WinForms's Datagrid

D

David Krmpotic

Hello,


Can please tell me how to do it ? This should be simple, because I'd think
it's used a lot.. but in reality is not that simple and there is at least 50
topics about it in newsgroups, but the problem is that people see the
question and just write about overriding OnPaint or pointing to the
syncfusion site without reading the question.. it's coloring the entire row,
not just one cell.. I can't believe it, but there is so many answers, but
not a single useful one. When I figure that out, I'll post it (if somebody
doesn't send the answer here).

Please, someone.. actually even if somebody provided a solution for this, I
need something more..
I need to colour the entire row based on specific underlying DataRow field
value.

for example:
myDataGrid.DataSource = myDataTable;

DataRows in DataTable are like this:
r["name"] = ...
r["lastname"] = ...
r["marked"] = true/false
..
..
..

but only first two are shown in datagrid, like:

DataGridColoredTextBoxColumn cs = new DataGridColoredTextBoxColumn();
cs.MappingName = "name";
cs.HeaderText = "Name";
myDataGrid.GridColumnStyles.Add(cs);

....


so.. I want to color red all the rows in DataGrid with "marked" set to true
in its DataSource's DataRows.

Please help.

your help would be most appreciated!

David
 
G

Guest

Don't know if this is the best solution, but it's one I use, and best of all,
it works...
You need to define define the 'ItemDataBound' for your datagrid, see sample
below:

private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView rv = (DataRowView)e.Item.DataItem;
if (rv.Row.ItemArray[2]) //check if 'marked'
{
e.Item.Cells[0].BackColor = Color.Red;
e.Item.Cells[0].ForeColor = Color.White;
e.Item.Cells[1].BackColor = Color.Red;
e.Item.Cells[1].ForeColor = Color.White;
e.Item.Cells[2].BackColor = Color.Red;
e.Item.Cells[2].ForeColor = Color.White;
}
}
}
 
D

David Krmpotic

Thank you, but I think your solution is good only for WebForms and it won't
work on WinForms :(


Paul Horstink said:
Don't know if this is the best solution, but it's one I use, and best of all,
it works...
You need to define define the 'ItemDataBound' for your datagrid, see sample
below:

private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView rv = (DataRowView)e.Item.DataItem;
if (rv.Row.ItemArray[2]) //check if 'marked'
{
e.Item.Cells[0].BackColor = Color.Red;
e.Item.Cells[0].ForeColor = Color.White;
e.Item.Cells[1].BackColor = Color.Red;
e.Item.Cells[1].ForeColor = Color.White;
e.Item.Cells[2].BackColor = Color.Red;
e.Item.Cells[2].ForeColor = Color.White;
}
}
}


David Krmpotic said:
Hello,


Can please tell me how to do it ? This should be simple, because I'd think
it's used a lot.. but in reality is not that simple and there is at least 50
topics about it in newsgroups, but the problem is that people see the
question and just write about overriding OnPaint or pointing to the
syncfusion site without reading the question.. it's coloring the entire row,
not just one cell.. I can't believe it, but there is so many answers, but
not a single useful one. When I figure that out, I'll post it (if somebody
doesn't send the answer here).

Please, someone.. actually even if somebody provided a solution for this, I
need something more..
I need to colour the entire row based on specific underlying DataRow field
value.

for example:
myDataGrid.DataSource = myDataTable;

DataRows in DataTable are like this:
r["name"] = ...
r["lastname"] = ...
r["marked"] = true/false
..
..
..

but only first two are shown in datagrid, like:

DataGridColoredTextBoxColumn cs = new DataGridColoredTextBoxColumn();
cs.MappingName = "name";
cs.HeaderText = "Name";
myDataGrid.GridColumnStyles.Add(cs);

....


so.. I want to color red all the rows in DataGrid with "marked" set to true
in its DataSource's DataRows.

Please help.

your help would be most appreciated!

David
 
D

David Krmpotic

Thank you!
I too stumbled upon this one just a second ago and it seems that it solves
the general problem..
I still have to dwelve into the code, because it's not that trivial.. I just
hope I'll be able to do the same not depending just on one column value, but
on underlying datarow. thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top