Hi George,
Thanks for posting in the community! My name is Steven, and I'll be
assisting you on this issue.
From your description, you has a DataGrid which contains a template column
with a radiobuttonlist in it.
Also you want to set the radionbuttonlis t 's item value and select
attribute at runtime during
the time when DataGrid's column is being bind to the data retrieved from
database, yes?
If there is anything I misunderstood, please feel free to let me know.
As for this problem, I think we can use the DataGrid's DataItemBound event
to implement our requirement. In the DataItemBound event, we can retrieve
the certain row which is currently being bund with datas from the
DataSource. Also, we can retrieve the certain row of DataSource. For our
siutation, we want to do some modification on the radiobuttonlist in the
template collumn, we can first retrieve the RadioButtonList control from
the column using "FindContro l" and then do operations on it. For example:
private void dgMain_ItemData Bound(object sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if(e.Item.ItemT ype == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
DataRowView drv = (DataRowView)e. Item.DataItem;
System.Web.UI.W ebControls.Radi oButtonList lst =
(System.Web.UI. WebControls.Rad ioButtonList)e. Item.Cells[2].FindControl("r blB
ind");
lst.Items[0].Text = drv["grade"].ToString();
lst.Items[0].Value = drv["grade"].ToString();
lst.SelectedInd ex = 0;
}
}
#dgMain is the DataGrid in a page
To make such approach clearly, here is a sample page I've made, you can
refer to it if you have anything unclear in my above description:
---------------------------------aspx page----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>RadioBut tonList</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
<asp:DataGrid id="dgMain" runat="server" AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="inde x"
HeaderText="ID" ></asp:BoundColumn >
<asp:BoundColum n DataField="name "
HeaderText="Nam e"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Bou ndList">
<ItemTemplate >
<asp:RadioButto nList id="rblBind" runat="server">
<asp:ListItem Value="BindItem ">BindItem</asp:ListItem>
<asp:ListItem Value="ItemA">I temA</asp:ListItem>
<asp:ListItem Value="ItemB">I temB</asp:ListItem>
<asp:ListItem Value="ItemC">I temC</asp:ListItem>
</asp:RadioButton List>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
-----------------------------code behind page class------------------
public class RadioButtonList : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data Grid dgMain;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
DataTable tb = new DataTable();
tb.Columns.Add( "index");
tb.Columns.Add( "name");
tb.Columns.Add( "grade");
for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();
newrow["index"] = index.ToString( );
newrow["name"] = "name" + index.ToString( );
switch(index%3)
{
case 0:
newrow["grade"] = "low";
break;
case 1:
newrow["grade"] = "normal";
break;
case 2:
newrow["grade"] = "high";
break;
}
tb.Rows.Add(new row);
}
dgMain.DataSour ce = tb;
dgMain.DataBind ();
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.dgMain.Ite mDataBound += new
System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.dgMa in_ItemDataBoun d
);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void dgMain_ItemData Bound(object sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if(e.Item.ItemT ype == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
DataRowView drv = (DataRowView)e. Item.DataItem;
System.Web.UI.W ebControls.Radi oButtonList lst =
(System.Web.UI. WebControls.Rad ioButtonList)e. Item.Cells[2].FindControl("r blB
ind");
lst.Items[0].Text = drv["grade"].ToString();
lst.Items[0].Value = drv["grade"].ToString();
lst.SelectedInd ex = 0;
}
}
}
----------------------------------------------------------------------------
-------------------------
If you have any further questions, please feel free to post here.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)