如何更改正在编辑的文本的颜色。我有一个datagridview里面的值。我想要的是当用户编辑单元格内的文本时。它必须将文本更改为红色,我该怎么做?
private void Gridview_Output_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
{
if (Gridview_Output.Columns[e.ColumnIndex].Name == "FontOut")
{
Gridview_Output.Rows[e.RowIndex].Cells[3].Value = "FontOut";
DataGridViewCellStyle Cstyle = new DataGridViewCellStyle();
Cstyle.ForeColor = Color.Red;
}
}
答案 0 :(得分:0)
这适用于所有列:
private void Gridview_Output_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = Gridview_Output[e.ColumnIndex, e.RowIndex];
cell.Style.ForeColor = Color.Red;
}
如果您想限制它,只需添加如下支票:
if (cell.OwningColumn.Name == "yourColumnName")
cell.Style.ForeColor = Color.Red
如果您想在用户编辑单元格时保留颜色,但是使用与之前相同的值,请使用这两个事件(如果您的Tag
未使用):
private void DGV_Points_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridViewCell cell = DGV_Points[e.ColumnIndex, e.RowIndex];
cell.Tag = cell.Value != null ? cell.Value : "";
}
private void DGV_Points_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = DGV_Points[e.ColumnIndex, e.RowIndex];
if (cell.Tag != null && cell.Tag.ToString() != cell.Value.ToString())
cell.Style.ForeColor = Color.Red;
}
答案 1 :(得分:0)
所以你想用红色显示编辑过的值吗?
然后这样做..
object previousValue;
public Form1()
{
dgv.CellBeginEdit += dgv_CellBeginEdit;
dgv.CellEndEdit += dgv_CellEndEdit;
}
void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dgv[e.ColumnIndex, e.RowIndex].Value != previousValue)
dgv[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
}
void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
previousValue = dgv[e.ColumnIndex, e.RowIndex].Value;
}