experchange > dotnet.languages.* > dotnet.languages.csharp

NvrBst (12-28-07, 11:03 PM)
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
Chris Shepherd (12-31-07, 04:07 PM)
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.
NvrBst (12-31-07, 09:51 PM)
On Dec 31, 6:07 am, Chris Shepherd <c> wrote:
[..]
> 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
ebiweb (01-01-08, 07:53 AM)
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
NvrBst (01-01-08, 09:27 AM)
On Dec 31, 9:53 pm, "ebi...@gmail.com" <ebi> wrote:
> 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
Similar Threads