[英]Change text value with image based on row value in gridview using asp.net
我正在嘗試根據其值更改圖像的行數據。 例如,行值是“七”,則必須用鷹的圖像來更改它。
我嘗試了以下代碼,但收到錯誤消息“對象引用未設置為對象的實例”。
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中圖像存儲的數量。
提前致謝。
看起來這兩行會產生錯誤:
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)
將Image1
插入到GridView1
,您需要從第10列而不是第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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.