how to change the background color of a selected row in datagridview
-
In a gridview, how to change the background color of a selected row, on click of a cell in datagridview. Thanks in advance
Handle the CellClick event:
private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
Handle the CellClick event:
private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
Thanks for your reply. The code which you sent changing the back color of a row, wherever I click in the gridview. So entire grid resulted in red background color My issue is to change the selected background color. To describe more, If I click on first row, background color should change to Dark Gray color. after that, if I click on second row, first row should get reverted to previous color and second row background color should be in darkgray color.
-
In a gridview, how to change the background color of a selected row, on click of a cell in datagridview. Thanks in advance
Set your row default selection colour in your form design, or add the following during initialisation:
gridView.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Red; // or other colour
And add something similar to the following to your cell click event:
gridView.Rows[e.RowIndex].Selected = true;
-
Thanks for your reply. The code which you sent changing the back color of a row, wherever I click in the gridview. So entire grid resulted in red background color My issue is to change the selected background color. To describe more, If I click on first row, background color should change to Dark Gray color. after that, if I click on second row, first row should get reverted to previous color and second row background color should be in darkgray color.
Do you expect me to do everything for you? Keep a "I changed this" row index, and change it back. Then set the new row, and save the index. It's not exactly rocket science...
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
Handle the CellClick event:
private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
Set your row default selection colour in your form design, or add the following during initialisation:
gridView.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Red; // or other colour
And add something similar to the following to your cell click event:
gridView.Rows[e.RowIndex].Selected = true;
-
:laugh: Easy to type!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.