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