-1

我正在创建动态数据表并将其分配给 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();
4

2 回答 2

0

尝试查看以下链接

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

希望这可以帮助!

于 2012-10-25T15:35:25.217 回答
0

将单选按钮组作为 RadioButtonList 包含在您的 GridView 中

<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>

在后面的代码中

 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 事件的更多信息

于 2012-10-25T16:07:10.633 回答