2010-12-01 94 views
1

我有一個包含所有不同類型的數據成員的類,包括一個字符串列表。當我將數據綁定到GridView時,我希望能夠將字符串列表分離到GridView中的不同列中。這些字符串更像標誌,最多有3個標誌。如果沒有標誌適用,該列表可以是空的,或者它可以僅包含一個或兩個標誌。我怎樣才能將這些標記分離出來,放到不同的GridView列中?我需要在OnRowDataBound事件中執行它嗎?將列表內部的列表綁定到GridView

當前,我的aspx代碼看起來像這樣。我希望能夠根據標誌是否被引發來改變Image控件的ImageUrl。

<asp:TemplateField HeaderText="Tax" SortExpression="Tax"> 
    <ItemTemplate> 
     <asp:Image ID="imgTax" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

    <asp:TemplateField HeaderText="Compliance" SortExpression="Compliance"> 
    <ItemTemplate> 
     <asp:Image ID="imgCompliance" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

    <asp:TemplateField HeaderText="Accounting" SortExpression="Accounting"> 
    <ItemTemplate> 
     <asp:Image ID="imgAccounting" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

謝謝!

+0

所以,你需要以編程方式顯示/隱藏基於標記的存在,這些圖片? – jwiscarson 2010-12-01 18:55:16

回答

0

有什麼辦法讓你修改你的數據來將這些字符串轉換爲布爾值?以這種方式使用字符串會使我感到代碼味道。就我個人而言,我會將這些字符串轉換爲您用作網格數據源的類的布爾屬性,並在標記中修改其可見性屬性,而不是返回到數據庫以逐行選擇這些屬性,行的基礎上。無論是

,是的,你可以使用RowDataBound事件是這樣的:

yourGrid_RowDataBound(object sender, EventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     YourClass currentClass = (YourClass) e.Row.DataItem; 

     for (int i = 0; i < currentClass.stringFlags.Length; i++) 
     { 
      string currentFlag = currentClass.stringFlags[i]; 

      if (currentFlag == "Tax") 
      { 
       Image imgTax = (Image) e.Row.FindControl("imgTax"); 
       imgTax.Visbile = true; 
      } 
      else if (currentFlag == "Compliance") 
      { 
       Image imgCompliance = (Image) e.Row.FindControl("imgCompliance"); 
       imgCompliance.Visbile = true; 
      } 
      else if (currentFlag == "Accounting") 
      { 
       Image imgAccounting = (Image) e.Row.FindControl("imgAccounting"); 
       imgAccounting.Visbile = true; 
      } 
     } 
    } 
} 
+0

謝謝!這幫了很多。最初這些字符串是一個不同圖層中的枚舉,但由於某種原因,該圖層和GUI之間的字符串變成了字符串。我會看看我能做些什麼來改變這一點。 – mhenry 2010-12-01 20:07:11