C# DataGridView控件 修改一行或单元格的颜色
作者:互联网
1.winform datagridview更改选中行中选中单元格的背景或字体颜色
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{ if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected) { DataGridViewCell aa = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; aa.Style.ForeColor = Color.Red; aa.Style.BackColor = Color.LightGreen; } }
2.winfrom——DataGridView 选中某一行的事件
先将SelectionMode属性设置一下,改为fullrowselection. 然后给一个cellclick事件 注意:点击表头时也会触发此事件,在取值时要排除
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e) { //当点击表头部的列时,e.RowIndex==-1 if (e.RowIndex > -1) { this.txtUsername.Text = this.dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString(); this.txtPassword.Text = this.dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString(); this.txtPosition.Text = this.dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString(); this.txtStatus.Text = this.dataGridView2.Rows[e.RowIndex].Cells[6].Value.ToString(); this.txtName.Text = this.dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString(); } }
3.C#控件DataGridView通过一列的值改变整行颜色
在控件DataGridView的RowPrePaint事件:
private void DGV_staffList_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { for (int i = 0; i < this.DGV_staffList.Rows.Count; i++) { if (this.DGV_staffList.Rows[i].Cells["Column14"].Value.ToString() == "1") { this.DGV_staffList.Rows[i].DefaultCellStyle.ForeColor = Color.Red; } } }
转 : https://bbs.csdn.net/topics/390207713/
https://blog.csdn.net/Pei_hua100/article/details/111565976
https://blog.csdn.net/baidu_38995168/article/details/86629995
标签:控件,Rows,C#,Cells,单元格,Value,RowIndex,ToString,dataGridView2 来源: https://www.cnblogs.com/fps2tao/p/16482219.html