473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DataGridView (Keep Style/Color On Sort)

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
4 19765
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: thomasp | last post by:
I know this is a vague question, but I am not sure what information to give. I am using VB2005 beta and the datagridview is pulling data from one of three tables in an Access database. For some...
1
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private...
3
by: joey.powell | last post by:
I have a datagridview where I set its "DataSource" property to a DataSet and its "DataMember" property to a table within the DataSet. I need for the DataGridView to "sort" through the contents of...
3
by: Daniel Manes | last post by:
I need a strategy to debug this situation... I can't put all the code involved, but here are some of the critical lines with comments: ------------------------- Private _parentDataCell As...
2
by: tee | last post by:
Hi, How do i sort datagridview programmically, current i have the following code but i keep get the error saying "DataGridView control must be bound to an IBindingList object to be sorted.", ...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
1
by: Ben456 | last post by:
Hello, I'm trying to figure out how to keep my cell formatting the same even after I've clicked a column sort header. Basically I've programmatically changed forecolors and backcolors of...
7
Plater
by: Plater | last post by:
I am having trouble determining when my DataGridView object is sorting (based on a column header click). The idea is, in a large table, sorting the columns takes time, so I show a splash screen....
3
by: Andrus | last post by:
I have DataGridView in virtual mode containing 3500 rows. In code below, assigning to RowCount value to 3500 takes 8 seconds. CPU usage goes high at this time. Stepping by F11 into user code shows...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.