C#ASP.NET中DataTable中的动态单选按钮

时间:2012-10-25 15:30:31

标签: c# asp.net

我正在构建项目,我正在创建动态数据表并将其分配给gridview。我需要在表格的每一行添加一个单选按钮。但我厌倦了它并且无法让它发挥作用。有人可以帮忙吗....

if (dt == null)
{
    dt = new DataTable("Shedules");
    DataColumn c0 = new DataColumn("Radio", typeof(RadioButton));
    DataColumn c1 = new DataColumn("SID", typeof(System.Int32));
    DataColumn c2 = new DataColumn("FID", typeof(System.Int32));
    DataColumn c3 = new DataColumn("FROM", typeof(System.String));

    dt.Columns.Add(c0);
    dt.Columns.Add(c1);
    dt.Columns.Add(c2);
    dt.Columns.Add(c3);

    dt.PrimaryKey = new DataColumn[] { c1 };
}


foreach (AgentPortal.SriLankanWebService.Shedule shedule in air1Response)
{
    DataRow dr = dt.NewRow();

    RadioButton rb1 = new RadioButton();
    rb1.GroupName = "depart";
    rb1.ID = shedule.Sid.ToString();
    dr["Radio"] = rb1;

    dr["SID"] = (int)shedule.Sid;
    dr["FID"] = (int)shedule.Fid;
    dr["FROM"] = (String)shedule.FromLocation;
    dt.Rows.Add(dr);
}

GridView1.DataSource = dt;
GridView1.DataBind();

2 个答案:

答案 0 :(得分:0)

尝试查看以下链接

http://www.devasp.net/net/articles/display/549.html

希望这有帮助!

答案 1 :(得分:0)

在GridView

中包含单选按钮组作为RadioButtonList
<asp:GridView ID="GridView1" runat="server">  
<Columns>

   <asp:TemplateField>

      <asp:RadioButtonList runat="server" ID="scheduleList" DataTextField="ScheduleName"
          DataValueField="ScheduleID" />

      </asp:TemplateField>

   <asp:BoundField DataField="SId" HeaderText="SID" />
   <asp:BoundField DataField="FId" HeaderText="FID" />
   <asp:BoundField DataField="FROMLocation" HeaderText="FROM" />
</Columns>
</asp:GridView>

代码beind

 protected void Page_Load(object sender, EventArgs e)
 {
   GridView1.DataSource = air1Response; //bind your airResponse directly to GridView
   GridView1.DataBind();

 }

 protected void gvSchoolClasses_RowDataBound(object sender, GridViewRowEventArgs e)
 {
    RadioButtonList scheduleList = (RadioButtonList) e.Row.FindControl("scheduleList");
    scheduleList = air1Response;
    scheduleList.DataBind();
 }

详细了解GridView Events