我正在嘗試使用基於其值的圖像更改行數據。例如,行值是「七」,它必須用鷹的圖像改變。使用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中圖片商店的數量。
在此先感謝。
請還添加了'' 'GridView1'的一節' –
ekad
Sir Girdview1的列是從csv文件綁定的。 –
請查看下面我編輯的答案,我想我找到了原因。 – ekad