Hi! Does anyone knows how to add a radiobutton to a dynamic gridview that will be mutually exclusive?
"In the GridView, the radio buttons cannot be grouped and therefore are not mutually exclusive. That is, an end user is able to select multiple radio buttons simultaneously from the GridView."
One of the solutions I found searching is to add a template field with a HtmlInputRadioButton witch works as mutually exclusive but only in normal gridviews, in the dynamic ones, the mutually exclusive doesn't work.
Also, once I have the radioButton set up correctly...how can I identified whether that row has the radiobutton checked or not?
For the dynamic grid I have this:
for (int i = y; i <= count; i++)
{
TableRow tr = new TableRow();
// Create column 1
TableCell td1 = new TableCell();
// Create a label control dynamically
Label _lblItem = new Label();
_lblItem.ID = id;
_lblItem.Text = text;
// Add control to the table cell
td1.Controls.Add(_lblItem);
TableCell td2 = new TableCell();
// grid
SqlConnection mycn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand mySqlCommand = new SqlCommand("SELECT * from Category, mycn);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
GridView myGridView = new GridView();
myGridView.ID = idGrid;
myGridView.DataSource = myDataSet;
//add RadioButton
TemplateField rbtnColumn = new TemplateField();
rbtnColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "Select");
rbtnColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
myGridView.Columns.Add(rbtnColumn);
myGridView.DataBind();
// Add control to the table cell
td2.Controls.Add(myGridView);
// Add cell to the row
tr.Cells.Add(td1);
tr.Cells.Add(td2);
// Add row to the table.
tblDynamic.Rows.Add(tr);
// Add control to the table cell
td2.Controls.Add(myGridView);
// Add cell to the row
tr.Cells.Add(td1);
tr.Cells.Add(td2);
// Add row to the table.
tblDynamic.Rows.Add(tr);
}
The GridViewTemplate class:
public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
ListItemType templateType;
string columnName;
public GridViewTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
HtmlInputRadioButton ckh = new HtmlInputRadioButton();
switch (templateType)
{
case ListItemType.Item:
container.Controls.Add(rbt);
break;
}
}
}
Thanks a lot.