简体   繁体   中英

I want to change the color of datagrid in C#

I have this code, but it does't work:

 private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
 {
     dgv.CurrentCell.Style.BackColor = Color.Red;
 }

I want it to change back color, when click the cell.

This works fine but you can't see it as long as the cell is also selected .

By simply deselecting the current or all cells you can see the new color right away, ie before leaving the cell:

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
  dgv.CurrentCell.Style.BackColor = Color.Red;
  //  deselect  either just the one you just selected..:
  dgv.CurrentCell.Selected = false;
  // ..or deselect all cells:
  dgv.ClearSelection();
 }

Try using the following statement. This should help you dgv.CurrentCell.Style.SelectionBackColor=Color.Red

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM