2012-02-10 49 views
0

如果特定列有值,我想設置gridview行的顏色。但是我的DIVSTATUS中有一個關於nullreference的錯誤。當列設置特定值時,我設置GridView行的顏色時出錯

我的ASPX

 <asp:TemplateField HeaderText="Status"> 
      <ItemTemplate>     
       <div style="width:70px;" id="divStatus" runat="server"><%# Eval("DscStatus")%></div> 
      </ItemTemplate> 
     </asp:TemplateField> 

我的代碼隱藏

   if (GridView1.Rows.Count > 0) 
       { 
        for (int i = 0; i < GridView1.Rows.Count; i++) 
        { 
         HtmlContainerControl divstatus = (HtmlContainerControl)GridView1.Rows[i].FindControl("divstatus"); 
          if (divstatus != null) 
          { 
           if (divstatus.InnerText == "Andamento Project") 
           { 
            GridView1.Rows[i].BackColor = System.Drawing.Color.Navy; 
            GridView1.Rows[i].ForeColor = System.Drawing.Color.White; 
           } 
          } 
        } 
       } 

我的渲​​染HTML

<td> 
    <div style="width:70px;">Andamento Project</div> 
</td> 
+0

我想你應該設定他們'行Databound'事件 – V4Vendetta 2012-02-10 11:01:27

回答

2

。利用RowDataBound財產和下面做代碼...你不需要爲了獲得div狀態,您只需獲取數據類型,並在該數據類型中獲得您的值通過使用比的屬性值,你可以決定你的顏色屬性行

protected void grdCAPRate_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     yourtype obj= (yourtype)e.Row.DataItem; 

      if (obj.DscStatus == "Andamento Project")  
      e.Row.BackColor = System.Drawing.Color.Navy; 
      else 
      e.Row.ForeColor=System.Drawing.Color.White;         

    }     
} 
+0

當按鈕我的用戶點擊,我打電話給我的「loadGrid ' 方法。在這個方法中,我把我的代碼。如何在沒有RowDataBound事件的情況下做到這一點? RowDataBound我認爲會很慢,因爲我的GridView中有很多數據。 – 2012-02-10 11:12:01

+0

@Lucas_Santos - 嘿,比你的代碼還好,如果可能的話,使用標籤控制...和se標籤的文本值,而不是div .. – 2012-02-10 11:17:55

+0

我會嘗試使用標籤。讓我嘗試。 – 2012-02-10 11:35:47

0
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      HtmlContainerControl divstatus = (HtmlContainerControl) e.Row.FindControl("divstatus"); 
      if (divstatus != null) 
      { 
       if (divstatus.InnerText == "Andamento Project") 
       { 
        e.Row.BackColor = Color.Navy; 
        e.Row.ForeColor = Color.White; 
       } 
      } 
     } 
    } 
+0

rowdatabound當我的用戶點擊按鈕,我打電話給我的'loadGrid'方法。在這個方法中,我把我的代碼。如何在沒有RowDataBound事件的情況下做到這一點? RowDataBound我認爲會很慢,因爲我的GridView中有很多數據。 – 2012-02-10 11:14:02

+0

我試過你的代碼。您的代碼運行時沒有錯誤。使用Debug檢查你的代碼是否正確。一定有一些被忽視的東西。 – sinanakyazici 2012-02-10 12:21:51

相關問題