生成复选框列在网格视图新列 [英] 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..
推荐答案
如果您希望将新列添加到 GridView控件
你应该把它添加到它的数据源
。无论是从数据库,或者选择另一列如果您使用的内存数据表
作为这里 - 通过添加另一个的DataColumn
。但我相信,你已经添加列(选择
)。
If you want to add a new column to the GridView
you should add it to it's DataSource
. Either by selecting another column from the database or, if you use an in-memory DataTable
as here- by adding another DataColumn
. But i assume that you already have added the column(Select
).
然后用模板列
添加单选
和eval /其绑定到列。
Then use a TemplateField
to add the RadioButton
and eval/bind it to the column.
<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>
您可以使用的RowDataBound
根据数据源检查:
You can use RowDataBound
to check it according to the datasource:
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");
}
}
这篇关于生成复选框列在网格视图新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!