By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,914 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,914 IT Pros & Developers. It's quick & easy.

DataGridView (Keep Style/Color On Sort)

P: n/a
I have a log viewer. I sort the DataGridView by the Time Column and
then run a function to set all cell backcolors depending if the cell
above is different.

This works correctly, however, when I resort a column (by clicking any
of the column headers) then all the colors revert back to the
origional white background.

Is there a way to have the DefaultCellStyle for each cell follow the
value/cell on sort?

NB
Dec 28 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
NvrBst wrote:
I have a log viewer. I sort the DataGridView by the Time Column and
then run a function to set all cell backcolors depending if the cell
above is different.
A better solution IMO is simply to handle the CellFormatting event and
change the default cell style in there.

ie:

void dgv_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
// Example - Row formatting
if (e.ColumnIndex == 0) // so this only runs once per row formatting
{
DataGridViewRow row = dgv.Rows[e.RowIndex];
if (row.Cells["some_cell"].Value.ToString() == "A")
row.DefaultCellStyle.BackColor = Color.Red;
return;

}
}

You should be able to easily adapt that to your needs of formatting a
single column or cell instead of a single row.
Chris.
Dec 31 '07 #2

P: n/a
On Dec 31, 6:07*am, Chris Shepherd <c...@nospam.chsh.cawrote:
NvrBst wrote:
I have a log viewer. *I sort the DataGridView by the Time Column and
then run a function to set all cell backcolors depending if the cell
above is different.

A better solution IMO is simply to handle the CellFormatting event and
change the default cell style in there.

ie:

void dgv_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
* * // Example - Row formatting
* * if (e.ColumnIndex == 0) // so this only runs once per row formatting
* * {
* * * *DataGridViewRow row = dgv.Rows[e.RowIndex];
* * * *if (row.Cells["some_cell"].Value.ToString() == "A")
* * * * * row.DefaultCellStyle.BackColor = Color.Red;
* * * *return;

* * }

}

You should be able to easily adapt that to your needs of formatting a
single column or cell instead of a single row.

Chris.
Thank you for the reply :) I knew about the CellFormatting event
handler but I don't think I decribed my task well enough.

Basically I have a DataGridView (Colums = Time/type/D1/.../D#). I
load the table (from a file) and then sort by time. After that I
highlight some cells (D Section) depending if the cell above it is
different than the current cell (0-1 D's change per row).

After the cells are highlighted (BackColor = Color.Red) then I simply
want the cell to remember its backcolor even if the user decides to
sort the table by say "Type".
Problem I see with using Cellformatting is that when someone sorts by
type then the cell above the current cell isn't nessisarly suppose to
be red (its only suppose to be red if the table is sorted by time).

To do it using CellFormate Event way I'd need a hidden preprocessed
sorted dictionary that I can compare the rows Time cell with so I can
check the previous cell (in relation to time) and then color during
CellFormat (which would probably be the easiest way if there is no way
for the table to remember its individual cell style when a user sorts
by a different column).

I'll try out the dictionary method (I only deal with 5MB log files so
its not too bad making a seperate dictionary to use for checking, if
you see a better way that I should approch the problem though do
tell :)

Thanks

NB
Dec 31 '07 #3

P: n/a
hi ,
i think you should invoke(call) your background changing color
method in PAINT event of your datagridview .

i did this in one of my programs.

regards,
ebiweb
Jan 1 '08 #4

P: n/a
On Dec 31, 9:53*pm, "ebi...@gmail.com" <ebi...@gmail.comwrote:
hi ,
i think you should *invoke(call) *your background changing color
method in *PAINT event *of *your *datagridview .

i did this in one of my programs.

regards,
ebiweb
Ahh that may be a little bit faster (lower level); the CellFormating
method should be very simular to that I think thought.

What I ended up doing was making a dictionary that stored only the
cells that were to change (Time/Colum#). Eventhoug the table is 5MB's
in size (100,000's of cells) there are only about 100-500 cells that
need to be highlighted). The cellformating event checks to see if the
cell is in the Highlighted List and then colors if needed. That way I
don't completly dupicate the table.

Thank you all for you help.

NB
Jan 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.