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

1
  • You need to define each column in the DataGrid. The first will be the checkBox column. Then bind the rest as normal. Commented May 1, 2013 at 14:11

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.