有谁知道如何在datagridview单元格内突出显示或更改特定文本的颜色?不更改所有单元格的背面或前部颜色,仅更改单元格中的特定单词。我尝试在datagridview的Cell_Formatting事件中执行此操作,但我仅看到更改整个单元格颜色的选项。
我的编码是:
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;
}
}
}
谢谢你的建议。
我的datagridview ck.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;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句