Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. fetching radiobutton control value from gridview

fetching radiobutton control value from gridview

Scheduled Pinned Locked Moved ASP.NET
c++helptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karan_TN
    wrote on last edited by
    #1

    through means of below code i add radiobutton to my gridview.

    grdFeedback.DataSource = ds;
    grdFeedback.DataBind();

    foreach (GridViewRow row in grdFeedback.Rows)
    {
    if (row.Cells[3].Text == "" && row.Cells[4].Text == "" && row.Cells[5].Text == "" && row.Cells[6].Text == "" && row.Cells[7].Text == "")
    {
    TextBox txtAns = new TextBox();
    txtAns.ID = "txtAns";
    row.Cells[3].Controls.Add(txtAns);
    }
    else
    {
    for (int c = 3; c <= 7; c++)
    {
    if (row.Cells[c].Text != " ")
    {
    RadioButton rbtn = new RadioButton();
    string rbt = "rbtngrp";
    string opt = row.Cells[c].Text;

                                rbtn.ID = rbt + row.RowIndex + c;
                                rbtn.Text = opt.ToString();
                                rbtn.GroupName = rbt + row.RowIndex;
                                row.Cells\[c\].Controls.Add(rbtn);
                            }
                        }
                    }
                }
    

    how to check it whether the button is checked or not in an button click?. The below code i used. but its error : 'grdFeedback.Rows[0].Cells[4].Controls[2]' threw an exception of 'System.ArgumentOutOfRangeException' bool {System.ArgumentOutOfRangeException}

    ((CheckBox)(grdFeedback.Rows[0].Cells[4].Controls[2])).Checked

    R 1 Reply Last reply
    0
    • K Karan_TN

      through means of below code i add radiobutton to my gridview.

      grdFeedback.DataSource = ds;
      grdFeedback.DataBind();

      foreach (GridViewRow row in grdFeedback.Rows)
      {
      if (row.Cells[3].Text == "" && row.Cells[4].Text == "" && row.Cells[5].Text == "" && row.Cells[6].Text == "" && row.Cells[7].Text == "")
      {
      TextBox txtAns = new TextBox();
      txtAns.ID = "txtAns";
      row.Cells[3].Controls.Add(txtAns);
      }
      else
      {
      for (int c = 3; c <= 7; c++)
      {
      if (row.Cells[c].Text != " ")
      {
      RadioButton rbtn = new RadioButton();
      string rbt = "rbtngrp";
      string opt = row.Cells[c].Text;

                                  rbtn.ID = rbt + row.RowIndex + c;
                                  rbtn.Text = opt.ToString();
                                  rbtn.GroupName = rbt + row.RowIndex;
                                  row.Cells\[c\].Controls.Add(rbtn);
                              }
                          }
                      }
                  }
      

      how to check it whether the button is checked or not in an button click?. The below code i used. but its error : 'grdFeedback.Rows[0].Cells[4].Controls[2]' threw an exception of 'System.ArgumentOutOfRangeException' bool {System.ArgumentOutOfRangeException}

      ((CheckBox)(grdFeedback.Rows[0].Cells[4].Controls[2])).Checked

      R Offline
      R Offline
      Roman_wolf
      wrote on last edited by
      #2

      First of all, you shouldn't be finding controls by indices. For one thing, it is confusing. Moreover, if you changed the order of your columns, you'll end up breaking your code. Instead, use the row.FindControl method. Another thing to check for the row type, since a grid may have several row types (header/footer/data/..) So while you checkbox exists in the DataRow, it doesn't exist in the header row for example, which in turn probably causes your exception. So you should do something like this:

      foreach (GridViewRow row in grdFeedback.Rows)
      {
      if (row.RowType == DataControlRowType.DataRow)
      {
      RadioButton btn=row.FindControl("RadioButton_ID_Goes_Here") as RadioButton;
      bool isChecked = btn.Checked;
      //do stuff
      }
      }

      K 1 Reply Last reply
      0
      • R Roman_wolf

        First of all, you shouldn't be finding controls by indices. For one thing, it is confusing. Moreover, if you changed the order of your columns, you'll end up breaking your code. Instead, use the row.FindControl method. Another thing to check for the row type, since a grid may have several row types (header/footer/data/..) So while you checkbox exists in the DataRow, it doesn't exist in the header row for example, which in turn probably causes your exception. So you should do something like this:

        foreach (GridViewRow row in grdFeedback.Rows)
        {
        if (row.RowType == DataControlRowType.DataRow)
        {
        RadioButton btn=row.FindControl("RadioButton_ID_Goes_Here") as RadioButton;
        bool isChecked = btn.Checked;
        //do stuff
        }
        }

        K Offline
        K Offline
        Karan_TN
        wrote on last edited by
        #3

        thanks for ur immediate reply roman. but the this i have implemeted. but the same error.

        ((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked

        but it returns error : ((System.Web.UI.WebControls.CheckBox)(((System.Web.UI.WebControls.RadioButton)(grdFeedback.Rows[0].Cells[3].FindControl("rbtn03"))))) is null Also i dont know y its showing "System.Web.UI.WebControls.CheckBox" in error message. plz help me Thanks in advance

        R 1 Reply Last reply
        0
        • K Karan_TN

          thanks for ur immediate reply roman. but the this i have implemeted. but the same error.

          ((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked

          but it returns error : ((System.Web.UI.WebControls.CheckBox)(((System.Web.UI.WebControls.RadioButton)(grdFeedback.Rows[0].Cells[3].FindControl("rbtn03"))))) is null Also i dont know y its showing "System.Web.UI.WebControls.CheckBox" in error message. plz help me Thanks in advance

          R Offline
          R Offline
          Roman_wolf
          wrote on last edited by
          #4

          I'm not sure how you set your controls in the datagrid, but ((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked shouldn't be needed. Try this instead

          ((RadioButton)grdFeedback.Rows[0].FindControl("rbtn03")).Checked

          As a rule of thumb, you shouldn't rely on indexes to fetch controls. Also you need to make sure that the row is a data row, as the header row won't have the radiobutton

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups