How to Loop through the radio button in gridview

Posted by Ali under ASP.NET on 6/29/2010 | Views : 9082 | Status : [Member] | Replies : 3
Hi,

I want to loop through the all rows in gridview and get radio button selected row and save that row to database.

and when i load data from database to gridview, if in database value is true then enable that radio button in row.

ASP.NET, C#.

Thanks




Responses

Posted by: Nandy on: 7/2/2010 [Member] Starter

Up
0
Down
foreach (GridViewRow row in GridView1.Rows)
{
RadioButtonList rd1= ((RadioButtonList )row.FindControl("RadioButtonList1"));
if(rd1.checked==true)
{
Label lbl=(Label)row.FindControl("Column1")
string column1Value=lbl.Text;
Label lb2=(Label)row.FindControl("Column2")
string column2Value=lb2.Text;
Label lb3=(Label)row.FindControl("Column3")
string column3Value=lb3.Text;
Label lb4=(Label)row.FindControl("Column4")
string column4Value=lb4.Text;
//Pass these column values to query to command object and update the in database
}
}

Ali, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 1/24/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Ali


for (int cellIndex = 0; cellIndex < GridView1.Rows.Count; cellIndex++)
{
GridViewRow row = GridView1.Rows[cellIndex];
int questionNumber = Convert.ToInt32(cellIndex + 1);

RadioButtonList lst = ((RadioButtonList)row.FindControl("RadioButtonList1")) as RadioButtonList;
string answer = (lst != null) ? lst.SelectedValue.ToString() : String.Empty;



values.Add(questionNumber, answer);
IFormatProvider culture = new CultureInfo("fr-Fr", true);
string constr1;
constr1 = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
con = new SqlConnection(constr1);
con.Open();
string sql = "SELECT * FROM paper where qno="+ questionNumber +"";
cmd = new SqlCommand(sql, con);
rd = cmd.ExecuteReader();
if (rd.Read())
{
int ransid = Convert.ToInt32(rd.GetValue(9).ToString());
if (answer == "N/A")
{
nanswer = nanswer + 1;
lblnotanswered.Text = Convert.ToString(nanswer);


}
else if (Convert.ToInt32(answer)==ransid)
{
total = total + 1;
lbltotalmarks.Text = Convert.ToString(total);
}
else
{
wanswer = wanswer + 1;
lblwronanswer.Text = Convert.ToString(wanswer);
}
}



}
Thanks & Regard's
Prabhakar

Best Regard's
Prabhakar

Ali, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/24/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Check this link

http://programming.top54u.com/post/ASP-Net-GridView-Checkbox-Select-All-using-C-sharp.aspx

Thanks
Karthik
www.f5Debug.net

Ali, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response