Vb.net – How to add checkbox to datagrid in vb.net

asp.netcheckboxdatagridvb.netvisual studio 2012

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

Best Answer

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>