2017-10-09 115 views
0

我在解決方案瀏覽器的兩個圖像在該位置處顯示圖像 -DataGridView的細胞返回NULL,而試圖在數據網格細胞

〜/圖像/ Active.jpg & 〜/圖像/ Inactive.jpg

我想顯示這些圖像在我的網格視圖單元取決於單元格值,即「活動」或「無效」。

我至今嘗試下面給出:

GridView的代碼在頁面源文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="GridView1_DataBound1" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" > 
      <Columns> 
       <asp:BoundField DataField="ServerName" HeaderText="ServerName" SortExpression="ServerName" /> 
       <asp:BoundField DataField="Application" HeaderText="Application" SortExpression="Application" /> 
       <asp:BoundField DataField="Instance" HeaderText="Instance" SortExpression="Instance" /> 
       <asp:BoundField DataField="Environment" HeaderText="Environment" SortExpression="Environment" /> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:Image ID="imgID" imageurl="" runat="server" /> 
        </ItemTemplate> 
       </asp:TemplateField>     

      </Columns> 

代碼隱藏文件.aspx.cs:

foreach (GridViewRow row in GridView1.Rows) 
      {    

       if (row.Cells[4].Text == "Active") 
       { 
        Image img = (Image)row.FindControl("imgID"); 
        img.ImageUrl= ""; 
       } 
       else 
       { 
        Image img = (Image)row.FindControl("imgID"); 
        img.ImageUrl = "~/Images/Inactive.jpeg"; 
       } 
      } 

請注意,我不能使用rowdatabound。我已經寫了上面的代碼中的一個函數,我正在檢查一些東西,並將單元格值更新爲Active或Inactive。

出現的問題是DataGrid的Cell返回圖像的空值。

請建議解決方案來解決這個問題。

+0

爲什麼你分配空 - 'img.ImageUrl =「」;'爲活動?它應該是'「〜/ Images/Inactive.jpeg」;'? –

+0

爲什麼不能使用行數據綁定事件? – Saurabh

+0

@KarthickRaju,那真是空虛。但即使我把網址,它不工作。 –

回答

0

嘗試:

foreach (GridViewRow row in GridView1.Rows) 
     {    
      Image img = (Image)row.FindControl("imgID"); 

      if (img!=null) 
      { 
       img.ImageUrl= (row.Cells[4].Text == "Active") ? "~/Images/Active.jpg" : "~/Images/Inactive.jpg"; 
      }   
} 
+0

我試着用你的答案,但img仍然爲null。 –

0

您可以使用三元運算符綁定你IMAGEURL:

ImageURL='<%# Eval("Status") == "Active" ? "~/Images/Active.jpg" : "~/Images/Inactive.jpg" %>' 

注:Status是從您的數據源的值。


或者,你可以做同樣的事情在你的RowDataBound事件:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     Image img = (Image)e.Row.FindControl("imgID"); 

     img.ImageURl = (e.Row.Cells[4].Text == "Active") ? "~/Images/Active.jpg" : "~/Images/Inactive.jpg"; 
    } 
} 
+0

我不會將值存儲到數據庫中。我正在檢查服務器連接,如果它處於活動狀態,則顯示該服務器的gridview單元處於活動狀態。 –

+0

但這個'row.Cells [4] .Text'代碼意味着你應該首先綁定單元值,否則它將爲空/空。 – AsifAli72090

+0

是row.cells [4]。文本具有「ACTIVE」或「INACTIVE」,我可以在調試時看到單元格文本是否處於活動狀態。它工作正常。但在此行後,當我在調試中檢查img值時,它顯示爲空Image img =(Image)e.Row.FindControl(「imgID」); –

0

感謝所有爲你的答案。我通過下面的改變解決了問題。現在工作很好。 :

if (answer.Contains("INACTIVE")) 
         { 

          row.Cells[4].Text = string.Format("<img src='Images/Inactive.jpg'; height='30' width='30' />"); 


         } 
         else 
         { 
          row.Cells[4].Text = string.Format("<img src='Images/Active.jpg'; height='30' width='30' />"); 

         }