0

Hi guys, I having problem display out the value at the gridview. At the database it store the gender field in integer. For example 1 = Male and etc.
I need to display out the text at gridview column. I getting error input string was not correct. Please help

Protected Sub gvCustomer_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCustomer.RowDataBound
             If e.Row.RowType = DataControlRowType.DataRow Then

            Dim gender As Integer = Convert.ToInt32(e.Row.Cells(5).Text)

                If gender = 1 Then
                    e.Row.Cells(5).Text = "MALE"
                ElseIf gender = 2 Then
                    e.Row.Cells(5).Text = "FEMALE"
                End If

          End If
    End Sub
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Ramesh S
0

Line No 4 is causing error i guess.

 Dim gender As Integer = Convert.ToInt32(e.Row.Cells(5).Text)

Thats because e.Row.Cells(5).Text will have null value as it is not binded.

You can do it in two ways :
1.Insert direct inline code in aspx upon the condition like this :

<ItemTemplate>
     <%# (Convert.ToInt32(Eval("gender"))=="1"?"Male": "Female") %>
 </ItemTemplate>

2.Store gender value in hiddenfield like this :

In Aspx :

<ItemTemplate>
    <asp:Label ID="lblGender" runat="server"></asp:Label>
    <asp:HiddenField runat="server" ID="hdnGender" Value='<%# Eval("gender")%>' />
</ItemTemplate>

In Backend :

If e.Row.RowType = DataControlRowType.DataRow Then
        Label lblGender = (Label)e.Row.FindControl("lblGender")
        HiddenField hdnGender = (HiddenField)e.Row.FindControl("hdnGender")
        If hdnGender.value = 1 Then
            lblGender.Text = "MALE"
        ElseIf hdnGender.value = 2 Then
            lblGender.Text = "FEMALE"
        End If
End If

Code above might contain C# format and have to be changed in VB.

Hope it solves the issue.

0

The error could be occured in the following line.

Dim gender As Integer = Convert.ToInt32(e.Row.Cells(5).Text*)

Put a breakpoint and see if the value of e.Row.Cells(5).Text is a number or empty string. It seems the index of the data cells(GridView column) may not be 5 or returns a non numeric value for some rows.

Edited by Ramesh S

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.