2014-01-20 28 views
1

我正在嘗試使用基於其值的圖像更改行數據。例如,行值是「七」,它必須用鷹的圖像改變。使用asp.net根據gridview中的行值使用圖像更改文本值

我已經嘗試了下面的代碼,但得到一個錯誤「對象引用未設置爲對象的實例。」

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 

    If e.Row.RowType = DataControlRowType.DataRow Then 
     Dim myVal As String = e.Row.Cells(10).Text 
     If myVal = "1" Then 
      Dim img As Image = TryCast(e.Row.FindControl("Image1"), Image) 
      img.ImageUrl = "~\images\cat.GIF" 
     ElseIf myVal = "7" Then 
      Dim img As Image = TryCast(e.Row.FindControl("Image2"), Image) 
      img.ImageUrl = "~\images\eagle.GIF" 
     End If 
    End If 
End Sub 

下面是GridView的ASPX代碼:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    Height="672px" Width="1037px"> 
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <EditRowStyle BackColor="#999999" /> 
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
</asp:GridView> 

而且下面是代碼結合CSV文件的GridView:

Dim csvFileName As String = "DATA.csv" 

     'Change this to your csv file path 
     Dim pathofcsv As String = strFolder 
     Dim conString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + pathofcsv + ";Extensions=csv" 
     ds = New DataSet("MyDataSet") 
     Dim adp As New OdbcDataAdapter() 
     Using conn As New OdbcConnection(conString) 
      Using cmd As New OdbcCommand(Convert.ToString("SELECT * FROM ") & csvFileName, conn) 
       conn.Open() 
       adp.SelectCommand = cmd 
       adp.Fill(ds) 
      End Using 
     End Using 

     grid.DataSource = ds 
     grid.DataBind() 
     grid.Rows(0).Visible = False 


     'Rename Columns Names 
     With grid.HeaderRow 
      .Cells(0).Text = "ANIMAL NAME" 
      .Cells(1).Text = "VALUE NO." 
     End With 

當 「VALUE NO」。是my.resources中圖片商店的數量。

在此先感謝。

+0

請還添加了'' 'GridView1'的一節' – ekad

+0

Sir Girdview1的列是從csv文件綁定的。 –

+0

請查看下面我編輯的答案,我想我找到了原因。 – ekad

回答

1

貌似這兩條線產生的錯誤:

Dim img As Image = TryCast(e.Row.FindControl("Image1"), Image) 
Dim img As Image = TryCast(e.Row.FindControl("Image2"), Image) 

由於數據是從csv,你需要在ASPX代碼添加Image1

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    Height="672px" Width="1037px"> 
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <EditRowStyle BackColor="#999999" /> 
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
    <Columns> 
     <asp:TemplateField HeaderText="Image"> 
      <ItemTemplate> 
       <asp:Image ID="Image1" runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

,並刪除這一行:

Dim img As Image = TryCast(e.Row.FindControl("Image2"), Image) 

插入Image1GridView1後,喲ü將需要從第十列,而不是9日獲得myVal

Dim myVal As String = e.Row.Cells(11).Text 

爲了避免重複,你只需要If塊之前聲明img一次:

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     Dim myVal As String = e.Row.Cells(11).Text 
     Dim img As Image = TryCast(e.Row.FindControl("Image1"), Image) 
     If myVal = "1" Then 
      img.ImageUrl = "~\images\cat.GIF" 
     ElseIf myVal = "7" Then 
      img.ImageUrl = "~\images\eagle.GIF" 
     End If 
    End If 
End Sub 
+0

先生我已經嘗試過,但仍然返回相同的錯誤 –

+0

你能顯示一些來自csv的數據嗎? – ekad

+0

csv文件中的數據是數字,每個數字都有相同的圖像,但它不包含在csv文件中 –

相關問題