突出显示c#windows应用程序中数据网格视图单元格内的特定文本
本文关键字:单元格 视图 文本 网格 数据网 显示 c#windows 应用程序 数据 | 更新日期: 2023-09-27 18:19:48
有人知道如何在datagridview单元格内突出显示或更改特定文本的颜色吗?不更改所有单元格的前后颜色,但仅更改单元格中某个特定单词的前后颜色。我在datagridview的Cell_Formating事件中尝试过这样做,但我只看到了更改整个单元格颜色的选项。
我的编码是:
private void Form1_Load(object sender, EventArgs e)
{
RowColor();
}
private void RowColor()
{
DataGridViewRow dgvr = dataGridView1.Rows[0];
if (!string.IsNullOrEmpty(Convert.ToString(dataGridView1.Rows[0].Cells["status"].Value)))
{
if (dataGridView1.Rows[0].Cells["status"].Value != null)
{
dgvr.Cells["status"].Style.BackColor = Color.Red;
}
}
}
谢谢你的建议。
mydatagridviewck.imgur.com/3lKFa.png)
如您所知,您可以更改背景颜色:
dgvr.Cells["status"].Style.BackColor = Color.Red;
此外,您可以这样更改文本颜色:
dgvr.Cells["status"].Style.ForeColor = Color.Blue;
因此,您可以在CellFormatting
事件处理程序中更改单元格背景/文本颜色,如下所示:
private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "status" && e.Value is string)
{
string text = (string)e.Value;
if (text == "PAUSE")
{// Change the color
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.Blue;
}
}
}