4

My GridView contains 20 columns which are added programmatically (DataTable, DataColumn, DataRow and DataSet). Now I need to insert a checkbox column as the last column (21st column). How should I add it?

I tried adding with the usual Template Field (from design tab) in .aspx file but that adds a checkbox as the first column and not as the last one.

6
  • Do you mean say that columns are auto generated for the grid view? Commented Jul 22, 2011 at 11:10
  • No columns are not auto generated Commented Jul 22, 2011 at 11:11
  • no.. i have added columns and row using data-table, column & row Commented Jul 22, 2011 at 11:18
  • Is AutoGenerateColumns="true" or "false" for the GridView? Commented Jul 22, 2011 at 11:19
  • nothing has been mentioned for that.. Commented Jul 22, 2011 at 11:26

2 Answers 2

2

If you are binding your GridView using a DataTable, do this before you set the GridView DataSource.

dataTable.Columns.Add("Select", Type.GetType("System.Boolean"));

DemoGrid.DataSource = dataTable;
DemoGrid.DataBind();

foreach (GridViewRow row in DemoGrid.Rows)
{
    //check box is the first control on the last cell.
    CheckBox check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
    check.Enabled = true;
}

On an unrelated side note, please note that your asp:GridView is in fact AutoGenerated.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks..! but the checkbox that is being added are all disabled and unchecked.. any way to enable and check-uncheck?
0

Create a TemplateField for the column with the check, and add the checkbox at the bottom as a footertemplate, and turn on showing the footer via GridView.ShowFooter = true; A footer is a great place to put commonly available controls like this.

The common trick, if the footer is not an option, is to bind an empty row of data to the UI, which will have the checkbox.

HTH.

3 Comments

I think requirement is adding the last column as checkbox, not footer row
brian, but we dont have a footer row for each gridviewrow:). the only method is see for this problem is to append another column to the dataset of typr boolean that would guarantee a checkbox.
Oh I see it now, I was really dense. I didn't see the column part.

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.