您的位置:首页 > 其它

DataGridView左键选中行改变行背景色,右键选中行不改变背景色。左键选中行的背景色不变

2016-11-09 23:38 190 查看
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)//左键
{
//刷新上一次选中的行
this.dataGridView1.Rows[RowLeft].DefaultCellStyle.BackColor = Color.White;
RowLeft = e.RowIndex;

//选中新的行
this.dataGridView1.Rows[e.RowIndex].Selected = true;
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Aqua;
}
else if (e.Button == System.Windows.Forms.MouseButtons.Right)//右键
{
this.dataGridView1.Rows[e.RowIndex].Selected = true;
//当前活动单元格还保留在上一行的行索引值,所以必须进行赋值更改当前活动单元格的行索引
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  datagridview
相关文章推荐