简体   繁体   English

使用asp.net根据GridView中的行值更改图像的文本值

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

相关问题 基于asp.net C#中的当前日期更改Gridview行值 - Change Gridview row value based on current date in asp.net C# 如何根据特定的单元格值更改Gridview的行颜色? Asp.net - How to change Gridview's row color based on a specific cell value? Asp.net Asp.Net:更改GridView中的列的值 - Asp.Net: Change value of Column in gridview 如何在 asp.net 中更新该行时更改 GridView 的特定输入字段值 - How to change a particular input field value of a GridView while updating that row in asp.net 使用C#在asp.net中的gridview中单击该行的超链接字段时,检索该行的值 - Retrieve a value of a row when clicked hyperlink field of that row in a gridview in asp.net using c# 基于单元格值asp.net的更改的备用颜色gridview行 - alternate color gridview rows based on change of cell value asp.net asp.net gridview如果单元格行值相等,则 - asp.net gridview if cell row value is equal then ASP.NET GridView - 无法获取上一行中单元格的值 - ASP.NET GridView - Unable to get value of cell in previous row 当弹出窗口显示gridview垂直行的值时,如何使用jQuery从ASP.net中的gridview检索值 - How to retrieve value from gridview in ASP.net using jQuery, when popup is displaying the value of perticular row of gridview 根据 ASP.Net 中的值验证 GridView 中的文本框 - Validate the TextBoxes in the GridView based on a value in ASP.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM