Hi, I have a DataGridView with a lot of columns, so the user will typically use two displays for editing. I want the entire row of the currently selected cell to be highlighted with a different background color, so the user easily can see which row is being edited. The problem is that when the grid loads data (ie. the DataView changes filter), all rows are being highlighted, so the user have to enter and leave each row before they get their original color back. Normally, only one row should be highlighted. I have googled without finding anything that helps. I have tried different steps, including using the RowEnter event to highlight and RowLeave to reset usingDefaultCellStyle.BackColor, and the CellFormatting event with a variable that remembers the last row so it can be reset. Both have the same problem - in the beginning, all rows are highlighted.
private void dgvEditor_RowEnter(object sender, DataGridViewCellEventArgs e)
{
Console.WriteLine("Entering row " + e.RowIndex);
dgvEditor.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Khaki;
}
private void dgvEditor_RowLeave(object sender, DataGridViewCellEventArgs e)
{
dgvEditor.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Empty;
}
The code outputs only the following:
Entering row 0
Entering row 0
Still, all rows are highlighted. |
| s__ Tuesday, July 07, 2009 10:53 AM |
You probably have to suspend the event during initial load. Try something like:
suspendEvents = true; dgvEditor.DataSource = .... // or whatever you do to load the data into the grid suspendEvents = false;
and
// start of event handler if ( suspendEvents) return;
Btw. why is the event called at all during load, selection shouldn't be changed there ?? - Marked As Answer bys__ Tuesday, July 07, 2009 1:14 PM
-
|
| Wollinet Tuesday, July 07, 2009 12:58 PM |
I was afraid I had to do so, but I am not sure it is even possible (but I will certainly try :)
I might have been unclear regarding 'load' - I mean when table rows are loaded into the table. That happens every time the user changes filter for the DataView which the DataGridView is bound to - but only on row 0 (if I recall correctly). Still, all other rows are colored the same, even if the event is not triggered for those rows.
edit: It worked! Thank you all.
It turned out I didn't even need the Invalidate or Refresh either, so this makes my application alot smoother. - Marked As Answer bys__ Tuesday, July 07, 2009 1:18 PM
-
|
| s__ Tuesday, July 07, 2009 1:12 PM |
What about DataGridView.DefaultCellStyle.SelectionBackColor. That should exactly do what you want !
-- Wolfgang
|
| Wollinet Tuesday, July 07, 2009 11:42 AM |
I might be wrong here, but I want thewhole row to be highlighted, even ifonly a cell is selected(SelectionMode is the default RowHeaderSelect). The SelectionBackColor only applies to what is selected, right? |
| s__ Tuesday, July 07, 2009 11:54 AM |
Ok, you got me ;-). You can try the RowPrePaint event and check if the currently selected cell is in that row and change the back color for all cells in that row.
-- Wolfgang
|
| Wollinet Tuesday, July 07, 2009 12:00 PM |
The problem is that on loading rows (changing filter in DataView), only one row triggers the event that is doing the highlighting, but all the other rows still get the same color! |
| s__ Tuesday, July 07, 2009 12:02 PM |
Try this
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
DataGridView1.ClearSelection()
DataGridView1.Rows(e.RowIndex).Selected = True
End Sub
Edit:- After reading your new replies that were posted when my page was open, I'm now not sure if this is what you want..
Thanks - Omie Someone makes my heart skip a clock cycle ! |
| Omie Tuesday, July 07, 2009 12:02 PM |
Then I could just have used FullRowSelect, and in addition it doesn't work with keyboard. What I want, is to change the BackColor property, only on the row that has a cell currently selected. Is my question / problem unclear? |
| s__ Tuesday, July 07, 2009 12:07 PM |
You problem is more or less clear, but the solution obviously not ;-). Try the SelectionChanged event with the logic you described earlier (keep the previously selected row for changeing back the color, change the color) and add this.Invalidate() or this.Refresh() at the end to force a redraw of the grid.
-- Wolfgang
|
| Wollinet Tuesday, July 07, 2009 12:13 PM |
Nope, still no luck! Tried this:
private void dgvEditor_SelectionChanged(object sender, EventArgs e)
{
// Remove color on previous row
if (prevSelectedRow != null)
{
prevSelectedRow.DefaultCellStyle = null;
}
if (dgvEditor.CurrentRow != null)
{
dgvEditor.CurrentRow.DefaultCellStyle.BackColor = Color.Red;
prevSelectedRow = dgvEditor.CurrentRow;
this.Invalidate();
this.Refresh();
}
}
Still, when changing filter (ie. loading new rows), all rows are colored red. Thanks for your help so far :) |
| s__ Tuesday, July 07, 2009 12:23 PM |
You probably can't use DefaultCellStyle here. You have to loop through all cells of the row:
foreach( DatagridViewCell cell in dgvEditor.CurrentRow.Cells ) cell.Style.BackColor = Color.Red;
You can probably skip the Invalidate() and Refreh() then (and you definitely don't need both). |
| Wollinet Tuesday, July 07, 2009 12:31 PM |
hello s____ for changing the defaultcellstyle of Rowyou should use new defaultCellStyle like, if you want to apply on specific row say with no.3, DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle(); highlightCellStyle.BackColor = Color.Red; dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle; Refer following link, http://msdn.microsoft.com/en-us/library/1yef90x0.aspx |
| NareshG Tuesday, July 07, 2009 12:34 PM |
Or you can set SelectionBackColor of RowsDefaultCellStyle at design time andif you want to highlight full row then set selectionMode to FullRowselect |
| NareshG Tuesday, July 07, 2009 12:39 PM |
Wollinet: Still no luck! Well - it (still) works in respect to that the selected row is red, and rows get the default color when leaving them, but all rows are still red in the beginning! Also, it does not seem that the event gets triggered for every row when they are created - only one row is set to red, but still all rows becomes red.
NareshG: Creating a new DataGridViewCellStyle did not change anything, are you sure this is why I am experiencing problems?
This is my current SelectionChanged:
// Remove color on previous row
if (prevSelectedRow != null)
{
foreach (DataGridViewCell cell in prevSelectedRow.Cells)
{
cell.Style.BackColor = Color.Empty;
}
}
if (dgvEditor.CurrentRow != null)
{
/*DataGridViewCellStyle cs = new DataGridViewCellStyle();
cs.BackColor = Color.Red;
dgvEditor.CurrentRow.DefaultCellStyle = cs;*/
prevSelectedRow = dgvEditor.CurrentRow;
foreach (DataGridViewCell cell in dgvEditor.CurrentRow.Cells)
{
cell.Style.BackColor = Color.Red;
}
}
this.Invalidate();
this.Refresh();
|
| s__ Tuesday, July 07, 2009 12:46 PM |
Try to do like this,set SelectionBackColor of RowsDefaultCellStyle at design time andif you want to highlight full row then set selectionMode to FullRowselect. No need to handle seelctionChanged event. No need to handle SelectionChanged event. - Edited byNareshG Tuesday, July 07, 2009 12:59 PM
-
|
| NareshG Tuesday, July 07, 2009 12:56 PM |
You probably have to suspend the event during initial load. Try something like:
suspendEvents = true; dgvEditor.DataSource = .... // or whatever you do to load the data into the grid suspendEvents = false;
and
// start of event handler if ( suspendEvents) return;
Btw. why is the event called at all during load, selection shouldn't be changed there ?? - Marked As Answer bys__ Tuesday, July 07, 2009 1:14 PM
-
|
| Wollinet Tuesday, July 07, 2009 12:58 PM |
Please read the thread - I don't need (and want) the entire row to be selected, I just need to change the background color. |
| s__ Tuesday, July 07, 2009 1:09 PM |
I was afraid I had to do so, but I am not sure it is even possible (but I will certainly try :)
I might have been unclear regarding 'load' - I mean when table rows are loaded into the table. That happens every time the user changes filter for the DataView which the DataGridView is bound to - but only on row 0 (if I recall correctly). Still, all other rows are colored the same, even if the event is not triggered for those rows.
edit: It worked! Thank you all.
It turned out I didn't even need the Invalidate or Refresh either, so this makes my application alot smoother. - Marked As Answer bys__ Tuesday, July 07, 2009 1:18 PM
-
|
| s__ Tuesday, July 07, 2009 1:12 PM |