Friday, June 22, 2012

RadioButtonGroup Across Multiple Rows in a Gridview

At work, I have a grid view, and in that grid view, every row is a different option. .Net is too smart for its own good in this case, as it puts every radio button in its own group rather than allowing you to use the same group along multiple rows. As a result, when you go to use the radio buttons, they do not deselect the previous selected option. So I thought about this from my point of view. How would I break it down?  Here's what I came up with.

Loop through every row, and find the radio button you want, then turn it off. Go back to the radio button that was originally selected, and then select it. We can do this because we can go back to the sender, find it, and modify it. Here's the code that does it:

protected void rdoPolicy_CheckedChanged(object sender, EventArgs e)
      RadioButton sendingRB = (RadioButton)sender;
      sendingRB.Checked = true;


private void DeselectRBButtons()
      int x = 0;
      foreach (var gridviewRow in grdPolicies.Rows)
          var rb = (RadioButton)grdPolicies.Rows[x].FindControl("rdoPolicy");
            rb.Checked = false;

No comments:

Post a Comment