DataGrid - retaining colours when re-ordering

  • Thread starter Thread starter David Cartwright
  • Start date Start date
D

David Cartwright

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
 
I have always re-colored the grid on filtering and sorting.
The code is quick so it's not a big deal.
 
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda Liu said:
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 DataGridViewCellStyle
tmpStyle.BackColor = System.Drawing.Color.Red ' (or whatever)
tmpRow.DefaultCellStyle = tmpStyle

David C
 
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 DefaultCellStyle to this row. The
following is a sample.

using System.Collection.Generic;

List<int> keylist = new List<int>();
public Form1()
{
InitializeComponent();
this.dataGridView1.RowPrePaint += new
DataGridViewRowPrePaintEventHandler(dataGridView1_RowPrePaint);
}

void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs 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.dataGridView1.NewRowIndex)
{
int keyvalue =
Convert.ToInt32(((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundI
tem)[0]);

if (keylist.Contains(keyvalue))
{

this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
}

private void Form1_Load(object 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
 
Linda Liu said:
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 DefaultCellStyle 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
 
Back
Top