473,698 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGrid - retaining colours when re-ordering

Hi all,

I'm using a DataGrid to present a tabulated list (actually a list of users
logged in to my phone system) - it seemed the most appropriate control. As
stuff happens on the phone system, my program changes the colours of various
lines - so if someone's on the phone their line would turn red, when they
hang up it turns green, and so on.

The thing is, though, when you re-order the grid elements by clicking on the
column headers, the colours vanish.

Is there a cunning way to make a DataGrid remember what colour each line is,
or am I going to simply have to handle it myself by catching the re-order
event and restoring the colouring?

Thanks in advance,

David C
Oct 19 '06 #1
5 1808
I have always re-colored the grid on filtering and sorting.
The code is quick so it's not a big deal.

--
Jeff Allan
Programmer Analyst
"David Cartwright" wrote:
Hi all,

I'm using a DataGrid to present a tabulated list (actually a list of users
logged in to my phone system) - it seemed the most appropriate control. As
stuff happens on the phone system, my program changes the colours of various
lines - so if someone's on the phone their line would turn red, when they
hang up it turns green, and so on.

The thing is, though, when you re-order the grid elements by clicking on the
column headers, the colours vanish.

Is there a cunning way to make a DataGrid remember what colour each line is,
or am I going to simply have to handle it myself by catching the re-order
event and restoring the colouring?

Thanks in advance,

David C
Oct 19 '06 #2
Hi David,

Could you tell me what kind of application do you set up? Is it a Windows
or Web application?

What version of VS are you using? Is it VS2003 or VS2005?

I haven't found a way to set the background color of a specific row in a
Windows Forms DataGrid control. If your application is a Windows
application, do you implement this by some custom code?

I look forward to your reply.
Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 20 '06 #3
"Linda Liu [MSFT]" <v-****@online.mic rosoft.comwrote in message
news:Sp******** ******@TK2MSFTN GXA01.phx.gbl.. .
Could you tell me what kind of application do you set up? Is it a Windows
or Web application?
What version of VS are you using? Is it VS2003 or VS2005?
Sorry, should have said. It's a VS2005 Windows application.
I haven't found a way to set the background color of a specific row in a
Windows Forms DataGrid control. If your application is a Windows
application, do you implement this by some custom code?
Yup. I just find the row I want ("tmpRow" below - it's a DataGridViewRow ),
then apply a style:

tmpStyle = New DataGridViewCel lStyle
tmpStyle.BackCo lor = System.Drawing. Color.Red ' (or whatever)
tmpRow.DefaultC ellStyle = tmpStyle

David C


Oct 20 '06 #4
Hi David,

Thank you for your prompt response.

I performed a test based on your sample code and saw the same result as you
did. I set the back color of the second row in my data grid view to the red
color at first. When I clicked on a column header to re-sort the data grid
view, the red back color of the second row disappeared.

To retain the back color of a data row in the data grid view, I recommend
you to handle the RowPrePaint event of the data grid view. In the event
handler, you find out whether the data row currently being painted needs
setting back color and if yes, set the DefaultCellStyl e to this row. The
following is a sample.

using System.Collecti on.Generic;

List<intkeylist = new List<int>();
public Form1()
{
InitializeCompo nent();
this.dataGridVi ew1.RowPrePaint += new
DataGridViewRow PrePaintEventHa ndler(dataGridV iew1_RowPrePain t);
}

void dataGridView1_R owPrePaint(obje ct sender,
DataGridViewRow PrePaintEventAr gs e)
{
// add your code to determin whether the row currently being
painted needs setting back color
// below code is a sample. Assume that the first column in the
data table is the primary key column
if (e.RowIndex < this.dataGridVi ew1.NewRowIndex )
{
int keyvalue =
Convert.ToInt32 (((DataRowView) this.dataGridVi ew1.Rows[e.RowIndex].DataBoundI
tem)[0]);

if (keylist.Contai ns(keyvalue))
{

this.dataGridVi ew1.Rows[e.RowIndex].DefaultCellSty le.BackColor = Color.Red;
}
}
}

private void Form1_Load(obje ct sender, EventArgs e)
{
// add the value of the primary key to the collection keylist
keylist.Add(1);
}

Hope this helps.
If my sample isn't want you need or you have anything unclear, please feel
free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 23 '06 #5
"Linda Liu [MSFT]" <v-****@online.mic rosoft.comwrote in message
news:Ny******** ******@TK2MSFTN GXA01.phx.gbl.. .
To retain the back color of a data row in the data grid view, I recommend
you to handle the RowPrePaint event of the data grid view. In the event
handler, you find out whether the data row currently being painted needs
setting back color and if yes, set the DefaultCellStyl e to this row. The
following is a sample.
Hi Linda,

That's a much more elegant way than I was thinking of - many thanks!

David C
Oct 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3949
by: Els | last post by:
Hi, I would like an opinion on the following: I have a page which is made up of background-images with transparent linked images in front of it, which on hover show text in CSS popups. Due to appropriate alt text, anyone viewing the page with images turned off, will see the alt text instead, and on hover will see the popups.
8
2251
by: Doug Laidlaw | last post by:
I tried to grab an image from a Web page the other day. It turned out that the page was made up of three horizontal bands, and part of the image was in each. One band was a JPEG, another was a GIF and I have forgotten what the main page was. Apart from lining up the parts of the image, there was no discrepancy in colours between the two formats. Is that what "Web-safe" means? I thought that it just meant that the colours would be the...
2
1764
by: Savvas | last post by:
Hi I am trying to make my datagrid look a bit better than the typical one. How do i show the datagrid to have alternating colours e.g. like in windows media player v.10? Thanks Savvas
4
3607
by: Tim Marshall | last post by:
The report indicated in the send object command below has various colours in it which show nicely when the report is previewed and printed. However, when sent as an attachment to users without Access as per the statement below, no colours are displyed: DoCmd.SendObject acSendReport, "rptSideDetails", acFormatHTML, _ Me.txtAddress, "", , Me.txtSubject, Me.txtBody, -1 I would dearly love to have colours showing in an exported Html...
2
1609
by: Elliot M. Rodriguez | last post by:
I am not sure how to implement this. I have a DataGrid which is bound to a DataSet that the user creates by adding items from a dropdown box on my page. When the dropdownlist item is selected, a new DataRow is created, and in my ItemCreated event I add a DropdownList and a TextBox object in 2 separate columns of the grid. The idea is to retrieve the values specified from those 2 controls and update my DataSet with the values the user...
1
1142
by: ruca | last post by:
Hi, I have a datagrid that I want to set different styles for header and normal items. The problem is that I setup right in properties of datagrid, but nothing happens in design. I can't say why!!! Can you help me??? For example: I want that all borders of datagrid can be lightblue, but the border of datagrid header must be darkblue.
1
1963
by: Vijay Kerji | last post by:
Hi, I have a datagrid with dropdown list and Remove hyperlink in it as columns. When I remove a row from the datagrid, Dropdown list selection is retaining its previous value. i.e, removed Dropdown item selection is applied to the next dropdown list. Is there any way to fix this problem?
4
2232
by: RoMo | last post by:
I am using VB .NET (2003) for a project that includes a datagrid on one form that allows the user to switch to details on a second form. The datagrid was built in design mode with all the proper style information. The user types in a part of a company name and a database search returns matching data (via ADO) to a dataset. If more than 1 matching company was found, the datagrid is made visible and has its source and member assigned to the...
5
4032
by: Jameson | last post by:
Hi, I have a list of known colours, generated using: Dim colorNames As New System.Collections.Generic.List(Of String) For Each known As KnownColor In .GetValues(GetType(KnownColor)) Dim specific As Color = Color.FromKnownColor(known)
4
5582
by: Peted | last post by:
Hi I need to iterate through a winforms visible datagrid, in a legacy application, to update it a little bit to change the background colours of the grid. This seems to be more dificult than i expected as it does not appear the DataGridRows collection is exposed with DataGrid. Is there a way to/ or best way to iterate through the rows of a
0
8673
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
8601
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7716
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
5860
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
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
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.