Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-CE C# Forms DataGrid , +
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:
 
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 19-Feb-13 12:08pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
joshrduncan2012 at 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)

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100