[英]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.