Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi everyone!

I need some advice on how to keep a row highlighted (backcolor) after a column header is clicked to resort.

This is what I have right now:

C#
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if ((int)dataGridView1.CurrentRow.Cells["KEY"].Value == colorHighlightedRowIndex)
                {
                    dataGridView1.Rows[colorHighlightedRowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                }
            }

colorhighlightedrowindex is supposed to represent an ID of the record in the database. How can I check for that particular ID after a column header is selected?
KEY is a column in the table.

Google has been no help for the last 30 minutes.

Thanks everyone!
Posted

1 solution

You can do it by changing the default style of the row: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsdefaultcellstyle.aspx[^].

For more information on cell styles and presentation, please see: http://msdn.microsoft.com/en-us/library/1yef90x0.aspx[^].

—SA
 
Share this answer
 
Comments
joshrduncan2012 19-Feb-13 17:27pm    
Thanks SA! Is that supposed to stay in tact after clicking a column header? What I have now doesn't keep the yellow highlighting after a column header click.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900