How to add checkbox to datagrid in vb.net

烂漫一生 提交于 2019-12-25 01:35:26

问题


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


回答1:


you can add checkbox using template field

Set AutoGenerateColumns attribute to false.

Add Column tag to asp:DataGrid tag.

Now add itemtemplate inside columns

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


来源:https://stackoverflow.com/questions/16319629/how-to-add-checkbox-to-datagrid-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!