如何解决DatagridVIew单单元格格式化
试图在这个问题上找到答案,但没有找到对我有用的方法。 我需要根据不同的条件更改dataGridView中单个单元格的字体颜色(背景色等).Fof示例-此dataGridView中同一行的另一个单元格中的值。 我以前找到的所有解决方案都不能解决我这个问题。
下面是我解决这个问题的建议。
解决方法
这是我的解决方案: 您需要创建事件datagridView_cell_Formating:
并添加与此类似的代码:
private void datagridView_cell_Formating(object sender,DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Target_column")
{
if ((e.Value != null) && (dataGridView1.Rows[e.RowIndex].Cells["Condition_cell"].Value.ToString() == "value"))
{
e.CellStyle.ForeColor = Color.Yellow;
}
else if ((e.Value != null))
{
e.CellStyle.ForeColor = Color.Red;
}
}
}
希望这对某人有帮助。
,每个DataGridView都有一个DefaultCellStyle
。每个DataGridViewColumn
的DataGridView都可以有一个DefaultCellStyle
。如果为NULL,则使用DataGridView的DefaultCellStyle
。要查看该列使用的实际单元格样式,请使用InheritedCellStyle
类似地,每个DataGridViewCell都有一个Style
。如果为null,则使用单元格列的InheritedCellStyle
,因此,如果此列的值为DefaultCellStyle,
,则使用DefaultCellStyle
的{{1}}。 DataGridViewCell实际使用的样式位于属性DataGridView
中。
- 单元格的样式为非null:InheritedStyle等于此样式
- 单元格具有空样式,列具有非空默认样式:Cell.InheritedStyle等于列DefaultStyle
- 单元格具有null样式,列具有null DefaultStyle:Cell.InheritedStyle等于DataGridview样式
如果只想更改某个单元格X的单元格样式,只需设置DefaultCellStyle。您可以采用InheritedStyle的Clone并更改属性。
InheritedCellStyle
,
protected void OnRowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
string temp = dr[3].ToString().Trim();
int sub = int.Parse(temp);
foreach (TableCell cell in e.Row.Cells)
{
if (sub > 0)
{
e.Row.Cells[3].BackColor = Color.Coral;
}
if (sub > 25)
{
e.Row.Cells[5].BackColor = Color.Coral;
}
}
}
}
如果这没有帮助,请详细说明要求。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。