Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a gridview with a radiobuttonlist in one of the columns. The radiobutton list consist of Yes/No options where the users need to choose. I want to access the gridview row by row to get the selectedvalue of the radiobutton list but I do not have any idea how to access it one by one.

Anyone please help me?

This is my gridview code:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="QuestionNo" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="QuestionNo" HeaderText="QuestionNo" ReadOnly="True" 
                SortExpression="QuestionNo" />
            <asp:BoundField DataField="Question" HeaderText="Question" 
                SortExpression="Question" />
            <asp:TemplateField HeaderText="Answer">
                <ItemTemplate>
                    <asp:RadioButtonList ID="rb1" runat="server">
                        <asp:ListItem Value="1">Yes</asp:ListItem>
                        <asp:ListItem Value="0">No</asp:ListItem>
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
Posted
Updated 1-Apr-11 1:52am
v3

I agree with answer above, just a little enhancement.
Follow this way;
foreach (GridViewRow row in GridView1.Rows)
{
    RadioButtonList rbSelect = (RadioButtonList)row.FindControl("rbl");
    ////TO DO: Write the required code.

}


I could have provided this code in VB.Net, but we'll make it more easy, just follow this link below;
C# to VB.Net[^]
 
Share this answer
 
v2
Comments
snamyna 1-Apr-11 7:54am    
can i have the code in vb?
avigodse 8-Apr-11 10:25am    
Please see the improved answer.
Following is the way

GridviewRow gvr=GridView1.Rows[GridView1.SelectedIndex];
RadioButtonList rbl=gvr.findcontrol("rb1") as RadioButtonList ;
string result =  rbl.SelectedValue;
 
Share this answer
 
Comments
snamyna 1-Apr-11 6:28am    
Hye sekharkaza. Thanks for your quick response.

Can it loop until the last row? I have about 40 rows inside the gridview where each row i will analyze the Yes/No answer.
avigodse 1-Apr-11 7:12am    
See my answer below.
C#
foreach (GridViewRow gvr in GridView1.Rows)
           {

     RadioButtonList rb = gvr.FindControl("rb1") as RadioButtonList

string ans=rb.selectedvalue;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900