2016-03-02 64 views
1

我正在使用Visual Studio 2015和實體框架6.我有GridView,我希望status列顯示四種不同類型的圖像,具體取決於數據庫中該字段的字符串。我希望用模板完成。有條件圖像模板列

這裏是我的GridView

<asp:GridView ID="gvStatus" runat="server" Height="184px" 
     Width="1359px" AutoGenerateColumns="false" AllowSorting="true" > 
    <HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black"/> 
    <RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White"/> 
    <AlternatingRowStyle BackColor="#afadad" ForeColor="White" /> 

    <Columns> 
     <asp:CommandField HeaderText="" SelectText="Delete" ShowSelectButton="true" ControlStyle-ForeColor="White" /> 
     <asp:BoundField HeaderText="Status" DataField="Status" />  
    </Columns> 
</asp:GridView> 

如何獲得status列有四個不同的圖像顯示出來,依賴於什麼字符串呢?

回答

1

你可以做這樣的:

<Columns> 
    <asp:TemplateField > 
     <HeaderStyle Width="10%" />        
     <ItemTemplate> 
      <asp:Image ID="Image1" runat="server" ImageUrl='<%#GetImagePath(Eval("img").ToString())%>' />     
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 

而且在後面的代碼:

public string GetImagePath(object value) 
{ 
    if (string.IsNullOrEmpty(Convert.ToString(value))) return string.Empty; 
    var x = int.Parse(value.ToString()); 
    return x > 0 ? "Your Image Path" : "Default Image Path"; 
    //Here I show two different types of images depending on int value in that field from the database you can change it to four type with if-else and also depending on string value 
} 
+0

我正在一個異常錯誤。我認爲它可能在我的圖像路徑中(圖像在名爲images的文件夾中的解決方案中 return x> 0?「〜/ images/new.png」:「〜/ images/completed.png」;)不是確定。 – wiredlime2015

+0

@ wiredlime2015 ...嘗試使用''/images/new.png''代替。順便說一句,如果你的圖像路徑是正確的,這個解決方案應該適合你。 –

+0

它拋出一個http異常,但在這一行上 wiredlime2015