[英]Change text value with image based on row value in gridview using asp.net
I am trying to change row data with images based on it's value. 我正在尝试根据其值更改图像的行数据。 For instance the row value is "seven", it must be changed with an image of an eagle.
例如,行值是“七”,则必须用鹰的图像来更改它。
I have tried below codes but got an error "Object reference not set to an instance of an object." 我尝试了以下代码,但收到错误消息“对象引用未设置为对象的实例”。
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
Below is the Gridview aspx code: 下面是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>
And below is the codes for binding csv file to 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
Where "VALUE NO." 其中“ VALUE NO。” is the number of image stores in the my.resources.
是my.resources中图像存储的数量。
Thanks in advance. 提前致谢。
Looks like these two lines generate the error: 看起来这两行会产生错误:
Dim img As Image = TryCast(e.Row.FindControl("Image1"), Image)
Dim img As Image = TryCast(e.Row.FindControl("Image2"), Image)
Since the data is from a csv, you need to add Image1
in the aspx code: 由于数据来自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>
and delete this line: 并删除此行:
Dim img As Image = TryCast(e.Row.FindControl("Image2"), Image)
After inserting Image1
to the GridView1
, you will need to get myVal
from the 10th column instead of 9th: 将
Image1
插入到GridView1
,您需要从第10列而不是第9列获取myVal
:
Dim myVal As String = e.Row.Cells(11).Text
To avoid repetition, you only need to declare img
once before the If
block: 为了避免重复,您只需要在
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.