简体   繁体   中英

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:

<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:

 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." is the number of image stores in the 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:

<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:

Dim myVal As String = e.Row.Cells(11).Text

To avoid repetition, you only need to declare img once before the If block:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM