简体   繁体   English

在网格视图中为复选框列生成新列

[英]generating new column in grid view for checkbox column

i am using datatable as data source for grid view 我正在使用数据表作为网格视图的数据源

     DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("Select",typeof(bool));


    //
    // Here we add five DataRows.
    //
    table.Rows.Add(25, "Indocin", "David");
    table.Rows.Add(50, "Enebrel", "Sam");
    table.Rows.Add(10, "Hydralazine", "Christoff");
    table.Rows.Add(21, "Combivent", "Janet");
    table.Rows.Add(100, "Dilantin", "Melanie");


    GridView2.DataSource = table;


    GridView2.DataBind();   

at page load and i want to create new column in grid to add radio button 在页面加载时,我想在网格中创建新列以添加单选按钮

actually i want to submit row values for selected check boxes in database.. 实际上我想提交数据库中选定复选框的行值。

If you want to add a new column to the GridView you should add it to it's DataSource . 如果要向GridView添加新列,则应将其添加到DataSource Either by selecting another column from the database or, if you use an in-memory DataTable as here- by adding another DataColumn . 通过从数据库中选择另一列,或者,如果您按此方式使用内存中的DataTable ,则可以通过添加另一个DataColumn But i assume that you already have added the column( Select ). 但是我认为您已经添加了列( Select )。

Then use a TemplateField to add the RadioButton and eval/bind it to the column. 然后使用TemplateField添加RadioButton并将其eval / bind到列中。

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView2_RowDataBound" >
         <Columns>
            <asp:TemplateField HeaderText="Select">
                  <ItemTemplate>
                     <asp:RadioButton ID="rbSelect"   runat="server" />
                  </ItemTemplate>
            </asp:TemplateField>

You can use RowDataBound to check it according to the datasource: 您可以使用RowDataBound根据数据源对其进行检查:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var rbSelect = (RadioButton)e.Row.FindControl("rbSelect");
        var row = ((DataRowView)e.Row.DataItem).Row;
        rbSelect.Checked = row.Field<bool>("Select");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM