我有一个datagridview显示我在sql server数据库中的所有用户。 这个小代码用于绘制禁用用户的行(禁用用户我只需单击复选框) 检查或取消选中复选框后,颜色不会自动刷新,我必须在表单或其他单元格中的某处clic以使更改可见。 我可以在检查或取消选中后立即显示它们。
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string userid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
if (e.ColumnIndex > 7)
{
bool value = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
string colname = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();
obj.updatePlugins(colname, value, userid);
}
if (e.ColumnIndex >= 6 && e.ColumnIndex <= 7)
{
bool adm = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
string colname = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();
obj.updateAdmin(adm, userid, colname);
}
if (e.ColumnIndex < 6)
{
string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
string colname = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();
obj.updateUser(colname, value, userid);
}
FillData1();
}
private void dataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e) {
if (e.ListChangedType != ListChangedType.ItemDeleted)
{
DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
red.BackColor = Color.Red;
DataGridViewCellStyle white = dataGridView1.DefaultCellStyle.Clone();
white.BackColor = Color.White;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (Convert.ToBoolean(r.Cells["DisabledUsers"].Value))
{
r.DefaultCellStyle = red;
for (int i = 6; i < r.Cells.Count; i++)
{
r.Cells[i].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
r.Cells[8].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
}
}
else
{
r.DefaultCellStyle = white;
for (int i = 6; i < r.Cells.Count; i++)
{
r.Cells[i].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
r.Cells[8].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
}
}
}
}
}