在单击按钮(而不是单击单元格)时更改数据网格视图单元格格式
本文关键字:单元格 单击 数据 数据网 网格 格式 视图 按钮 | 更新日期: 2023-09-27 18:30:32
我正在为学校项目编写酒店预订管理器。如果酒店的客人已经入住或退房,我需要查克。如果客人没有被切入或切出写入数据库的日期,我需要更改行的颜色,以标记此预订需要注意。
我正在尝试找到在单击按钮时更改单元格格式的解决方案(现在,稍后它将是计划任务)。我知道如何在将数据加载到 dataGridView 时更改单元格格式。
我一直在尝试这个
dataGridView1.Rows[(int)myReader["id"]].Style.BackColor = System.Drawing.Color.LightPink;
但它说没有 Style 方法,我理解,因为它是来自 CellFormatting 类的方法。
我可以在 CellFormatting 类中编写代码,但我不能在单击按钮时调用它,因为我不知道在 () 之间为发件人和 e 写什么。
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
您可以使用DataGridViewRow
的属性DefaultCellStyle
为行设置一些样式,例如:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//if some criteria
row.DefaultCellStyle.BackColor = Color.Red;
}
您也可以在某些合适的事件中更改样式,例如,您可以RowPrePaint
并检查行上的一些条件并更改其样式:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
//you can find the row this way: this.dataGridView1.Rows[e.RowIndex]
//then check some criteria on some cell values
//then you can set the style for row this way:
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}