如何在 vb.net 中将复选框添加到数据网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16319629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add checkbox to datagrid in vb.net
提问by JPJedi
I have a datagrid with a set of columns showing data from a database. I create the datatable and add it to the datagrid and then bind the source. this works great and now I would like to add a column to the front of the grid that has checkbox in it.
我有一个包含一组列的数据网格,其中显示了来自数据库的数据。我创建数据表并将其添加到数据网格,然后绑定源。这很好用,现在我想在包含复选框的网格前面添加一列。
Do I add the checkbox when I am adding the new row to the datatable that is shown in the datagrid or after I databind the datatable to the datagrid?
当我将新行添加到数据网格中显示的数据表或将数据表绑定到数据网格之后,是否添加复选框?
Using: VB.Net, Visual Studio 2012
使用:VB.Net,Visual Studio 2012
回答by Shafqat Masood
you can add checkbox using template field
您可以使用模板字段添加复选框
Set AutoGenerateColumns attribute to false.
将 AutoGenerateColumns 属性设置为 false。
Add Column tag to asp:DataGrid tag.
将 Column 标签添加到 asp:DataGrid 标签。
Now add itemtemplate inside columns
现在在列内添加 itemtemplate
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>
and if you want to attach it to datatable column then u have to add like this
如果你想把它附加到数据表列那么你必须像这样添加
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkStatus_OnChackedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>

