0

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

JPJedi
  • 1,498
  • 7
  • 32
  • 58
  • You need to define each column in the DataGrid. The first will be the checkBox column. Then bind the rest as normal. – OneFineDay May 01 '13 at 14:11

1 Answers1

3

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>
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23