C# 如何根据条件更改DataGridview中的列前景色?
我有一个类似employee的表,其中一行是“status”。 如果状态值为“批准”,则我希望以绿色显示该行 否则我想用红色显示它。 我试过跟随,但不起作用C# 如何根据条件更改DataGridview中的列前景色?,c#,gridview,C#,Gridview,我有一个类似employee的表,其中一行是“status”。 如果状态值为“批准”,则我希望以绿色显示该行 否则我想用红色显示它。 我试过跟随,但不起作用 if (e.Row.RowType == DataControlRowType.Header) { string status = DataBinder.Eval(e.Row.DataItem, "IsApprove").ToString(); if (status == "pending") {
if (e.Row.RowType == DataControlRowType.Header)
{
string status = DataBinder.Eval(e.Row.DataItem, "IsApprove").ToString();
if (status == "pending")
{
e.Row.ForeColor = System.Drawing.Color.Red; // Change the row's Text color
}
}
还有这个
private void gvleavedetail_cellformatting(object sender, datagridviewcellformattingeventargs e)
{
// if the column is the artist column, check the
// value.
if (this.gvleavedetail.columns[e.columnindex].name == "artist")
{
if (e.value != null)
{
// check for the string "pink" in the cell.
string stringvalue = (string)e.value;
stringvalue = stringvalue.tolower();
if (stringvalue == "high")
{
e.cellstyle.backcolor = color.pink;
}
}
}
但在本例中,我得到了datagridviewcellformattingeventargs的错误
我使用的是VS2010嗨,我得到的所有解决方案,请看这一个;) 只需在GRIDVIEW的RowDataBound事件上编写以下条件代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IsApprove"));
if (status == "pending")
{
e.Row.Cells[7].ForeColor = System.Drawing.Color.Yellow;
}
else if(status== "accept")
{
e.Row.Cells[7].ForeColor = System.Drawing.Color.Green;
}
else if (status == "reject")
{
e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
}
}
}