Search code examples
asp.netvb.netgridviewrowdatabound

GridView RowDataBound not running code


I have Ref table and patientDetails in sql. for example in Ref I have Gender column with values (Female, Male etc), In patientDetails the Gender column is 24 and I am binding the data to patientDetails and I have 24 in the Gender column in the Gridview but it should be Female. ( Foreign key and navigation properties would not work because I have so many different rows in Ref table like ethnicity,religion etc). I was told RowDataBound will work so I have this method now but its not going past the If statement line..

More Details:

my Data Source is binding to PatientDetails table which has Gender column ( with number values - which is getting it from ref table). I mean I have Ref table ( Like a lookup for everything - with columns ID, name, Description) In name I have gender, Ethnic group, religion) In Description Column I have the values for instance for gender: male, female etc however in the Gender column in Patient is returning the corresponding Ref ID of the value - for female it will store 24 in Gender column in Patient) so my Gridview is bounding correctly and getting whats stored in PatientDetails table however having 24, 25 in Gender is not helpful to show so I was told RowDataBound Can bring the correct correponding value in Ref table

 Protected Sub gvPatientDetails_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim lblGender As Label = e.Row.FindControl("lblGender)
    End If
End Sub


 <asp:GridView ID="gvPatientDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvPatientDetails_RowDataBound">

<Columns>
            <asp:TemplateField HeaderText="Gender">
                <ItemTemplate>
            <asp:Label ID="lblGender" runat="server" Text='<% #Eval("lblGender") %>'></asp:Label>
                </ItemTemplate>
  </asp:TemplateField>
        </Columns>
    </asp:GridView>

Solution

  • OK, I got it working.

    For all the beginners who might struggle with this, the above code is working as it should, when I debug it first time it just hit the IF statement then doesnt go inside ( because the first one is Header (I know duh!!) hit continue and it should give you the actual value of label then after that it should be simple. Just getDataFromRef ( a method to get data from Ref - id = lblGender ) then

    lblGender.Text = ( The value from method - getDataFromRef ).
    

    Basically I wasn't aware the debugger will start with the Header then when you hit continue will go through the gridview rows!

    All the best all, happy coding!:)