2010-12-28 27 views
2

従業員のようなテーブルが1つあり、その行の1つが「ステータス」です。 ステータス値が 'approve'の場合は、その行を緑色で表示したい それ以外の場合は赤色で表示します。 私は、次の試してみましたが、それは条件ごとにDataGridviewの列の色を変更するにはどうすればよいですか?

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 
    } 
} 

も、このONE

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; 
      } 
     } 
    } 

しかし、この場合には、私はVS2010

答えて

3

こんにちはすべてを使用していdatagridviewcellformattingeventargs のエラーを取得していますが動作しません私は解決策を得ました;)

ただ、次の条件コードをRowDataBound EVENT OF GRに書きますIDVIEW

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; 
      } 
     } 
    } 
関連する問題