-1

问题是,当我在表单构造函数中通过 RowTemplate.DefaultCellStyle.SelectionBackColor 更改所选行的颜色时,它可以工作,但是当用户单击某些按钮以更改网格选择的背景颜色时,它在按钮事件中不起作用!请有任何帮助!

public Form1()
{
   InitializeComponent();
   dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Red; //this      works fine
}
void button2_Click(object sender, EventArgs e)
{
  dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Blue;//but this does not work
}
4

2 回答 2

1

尝试这个 ..

void dataGridView1_RowPrePaint(object sender,
    DataGridViewRowPrePaintEventArgs e)
{
  If (DatagridView1.Rows(DataGridView1.CurrentCell.RowIndex).Selected )
  {

     DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).DefaultCellStyle.SelectionBackColor=Color.Blue;

  }
}
于 2013-06-20T07:00:10.883 回答
0
void button2_Click(object sender, EventArgs e)
{
   foreach (DataGridViewRow row in dataGridView1.Rows)
   {
       row.DefaultCellStyle.SelectionBackColor = Color.Blue
   }
}

更新 :

void button2_Click(object sender, EventArgs e)
{
   foreach (DataGridViewColumn col in dataGridView1.Columns)
   {
       col.DefaultCellStyle.BackColor = Color.Blue
   }
}
于 2013-06-20T08:25:42.357 回答