DataGridView根据某一列值改变行颜色,需要同时用到事件:CurrentCellDirtyStateChanged 和 CellValueChanged
private void dgv1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= dgv1.Rows.Count - 1 || e.RowIndex < 0) return; DataGridViewColumn col = this.dgv1.Columns["C_check"]; var row = dgv1.Rows[e.RowIndex]; if (Convert.ToBoolean(row.Cells[col.Index].Value) == true) { row.DefaultCellStyle.BackColor = Color.LemonChiffon; } else { row.DefaultCellStyle.BackColor = Color.White; } } private void dgv1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dgv1.IsCurrentCellDirty) { dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit); } }
参考:C# DataGridViewCheckBoxColumn编辑时实时触发事件
标签:C#,DataGridView,dgv1,RowIndex,一列,WinForm,row From: https://www.cnblogs.com/han-zhonggong/p/16699107.html