473,767 Members | 7,178 Online
Bytes | Software Development & Data Engineering Community
+ 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 DefaultCellStyl e for each cell follow the
value/cell on sort?

NB
Dec 28 '07 #1
4 19817
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_CellFormatt ing(object sender,
DataGridViewCel lFormattingEven tArgs 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.DefaultCell Style.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.ch sh.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_CellFormatt ing(object sender,
DataGridViewCel lFormattingEven tArgs e)
{
* * // Example - Row formatting
* * if (e.ColumnIndex == 0) // so this only runs once per row formatting
* * {
* * * *DataGridViewRo w row = dgv.Rows[e.RowIndex];
* * * *if (row.Cells["some_cell"].Value.ToString () == "A")
* * * * * row.DefaultCell Style.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.c om" <ebi...@gmail.c omwrote:
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
11777
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 reason when I edit the feilds that I have not set as readonly, the changes do not get written to the database. I have the code that loads the datagridview posted below. Thanks for any suggestions. Thanks, Thomas
1
3162
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 WithEvent DataTable as New DataTable
3
11944
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 the DataSet as it binds. Can I specify a column on the DataSet that, sorted, will control the order of the rows in the datagridview? It seems to just parse over the data in the DataSet and bind to it, without my being able to control the order...
3
4654
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 DataGridViewCell 'declare private field _parentDataCell = _parentDataGrid.Rows(rowIndex).Cells(columnIndex) 'set to a specific cell Debug.Print(_parentDataCell.DataGridView.ToString) 'prints:
2
29661
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.", What is IBindingList and how would i go about using it. Here are my code private void dataGridView1_ColumnHeaderMouseClick(object sender,
7
15658
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 there an equivalent property for the DataGridView? I have searched, but have not found one. I would like the user to be able to see all the columns of the table on one screen - thus eliminating the need to use the horizontal scroll bar to view...
1
5137
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 several different cells in my datagridview. When I the click the sort header it resorts the data but all my color formatting is lost. Is there a way to not lose all this formatting when I sort?
7
20038
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. When it is done sorting I want the splash screen dissapear. What I had been doing was using the CellClick and Sorted events: private void myDGV_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) {//sorting
3
5744
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 few celltemplate property getters and combobox/datecombo constructor calls without database access which does not take a lot of time. Debug output (below) shows lot of messages Stepping over non-user code. Running in release mode from...
0
9575
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9960
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8840
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6656
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3931
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 we have to send another system
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2808
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.