Jump to content
  • Advertisement
Sign in to follow this  
faculaganymede

[.net] C# DataGridView DataSet and table row color

This topic is 4237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C# gurus, Is there a way to set/save the row color inside the DataSet table? More explanation: I use DataSet to set up a data table and bind it to DataGridView. I want to set specific ForeColors for certain rows in the table based on a row cell value. This is the way I know how: DataGridViewRow row; row = dataGridView1.Rows; row.DefaultCellstyle.ForeColor = Color.Red; This doesn't seem to be the best way, because the color setting only applies to DataGridView, doesn't get stored inside the DataSet table. So if I do something like filtering the rows and re-binding the data table, like this: DataView dv = new DataView(tbl.Tables[0]); dv.RowFilter = "CODE=1"; dataGridView1.DataSource = dv; ...then it's necessary to re-apply the row colors because it's not done automatically.

Share this post


Link to post
Share on other sites
Advertisement
Here's a sample from one of my apps.


for (int r = 0; r < noOfRows; r++)
{
for (int c = 0; c < noOfColumns; c++)
{
if (newBytes[index] != oldBytes[index])
{
dataGridView1.Rows[r].Cells[c].style.ForeColor = Color.Red;
}
else
{
dataGridView1.Rows[r].Cells[c].style.ForeColor = Color.Black;
}
}
}


EDIT: This only works after the cells have been added to the DataGridView.

Share this post


Link to post
Share on other sites
Thanks CodeReaver.

This is similar to how I did it also, via the DataGridView cell stle. But, I am really interested to know if color can be applied to the row elements in the DataSet table before the table is added to DataGridView.

May be it's not possible because I don't see any methods in DataSet for setting cell colors. Thanks.

Share this post


Link to post
Share on other sites
Why on earth would DataSet contain anything that had to do with how the data is presented to the user? Such a thing is not a concern for a DataSet whose sole purpose in life is managing a set of... data.

No, you'll have to implement this mostly for yourself, especially since the behavior is non-trivial behavior (the colors are context sensitive).

Share this post


Link to post
Share on other sites
I'd just like to second Washu's incredulity. A data set (or a DataSet) is for data, not presentation.

The way CodeReaver and you are doing it now is fine.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!