Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is the aspx code for my page

XML
<asp:GridView ID="examgrid" runat="server" AutoGenerateColumns="false"
        AllowPaging="true" OnPageIndexChanging="examgrid_pageindexchanging"
        onrowdatabound="examgrid_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Exam">
<ItemTemplate>
<asp:Label ID="quesname" runat="server" Text='<%Eval(Ques_Name) %>'></asp:Label>
<br />
<asp:RadioButton ID="op1" runat="server" Text='<%Eval(Option1) %>' /><br />
<asp:RadioButton ID="op2" runat="server" Text='<%Eval(Option2) %>' /><br />
<asp:RadioButton ID="op3" runat="server" Text='<%Eval(Option3) %>' /><br />
<asp:RadioButton ID="op4" runat="server" Text='<%Eval(Option4) %>' /><br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


can any tells me how do i can show the values from database into radio buttons and in label which is reside into gridview in a single column

i use that code in c#

C#
for (int i = 0; i <= dt.Rows.Count - 1; i++)
        {
            string ques = dt.Rows[i][0].ToString();
            string opt1 = dt.Rows[i][1].ToString();
            string opt2 = dt.Rows[i][2].ToString();
            string opt3 = dt.Rows[i][3].ToString();
            string opt4 = dt.Rows[i][4].ToString();
            examgrid.Rows[i].Cells[0].Text = ques;
            
                    RadioButton op1 = (RadioButton)row.FindControl("op1");
                    RadioButton op2 = (RadioButton)row.FindControl("op2");
                    RadioButton op3 = (RadioButton)row.FindControl("op3");
                    RadioButton op4 = (RadioButton)row.FindControl("op4");
                    op1.Text = opt1;
                    op2.Text = opt2;
                    op3.Text = opt3;
                    op4.Text = opt4;
         }

but in tht code radio shows null each one,

can anyone help me in it
Posted
Updated 18-Dec-12 2:00am
v2
Comments
In which GridView event you are trying to do this?
You are doing like (RadioButton)row.FindControl("op1");, so can u show the code where row is assigned value?

1 solution

Please recheck your code and try with following. Please put #Eval("") and name of the column.
ASP.NET
<asp:gridview id="examgrid" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
        AllowPaging="true" OnPageIndexChanging="examgrid_pageindexchanging"
        onrowdatabound="examgrid_RowDataBound">
<columns>
<asp:templatefield headertext="Exam">
<itemtemplate>
<asp:label id="quesname" runat="server" text="<% #Eval("Ques_Name") %>"></asp:label>
<br />
<asp:radiobutton id="op1" runat="server" text="<% #Eval("Option1") %>" /><br />
<asp:radiobutton id="op2" runat="server" text="<% #Eval("Option2") %>" /><br />
<asp:radiobutton id="op3" runat="server" text="<% #Eval("Option3") %>" /><br />
<asp:radiobutton id="op4" runat="server" text="<% #Eval("Option4") %>" /><br />
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>



on code behind

static DataTable dt;
   protected void Page_Load(object sender, EventArgs e)
   {
       dt = new DataTable();
       dt.Columns.Add("Ques_Name");
       dt.Columns.Add("Option1");
       dt.Columns.Add("Option2");
       dt.Columns.Add("Option3");
       dt.Columns.Add("Option4");
       DataRow dr = dt.NewRow();
       dr[0] = "Name";
       dr[1] = "A";
       dr[2] = "B";
       dr[3] = "C";
       dr[4] = "d";
       dt.Rows.Add(dr);
       examgrid.DataSource = dt;
       examgrid.DataBind();

   }


Please feel free to ask any query.
 
Share this answer
 

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