开发者

Insert checkbox column at the end of gridview

开发者 https://www.devze.com 2023-03-22 09:56 出处:网络
My GridView contains 20 columns which are added programmatically (DataTable, DataColumn, D开发者_Python百科ataRow and DataSet). Now I need to insert a checkbox column as the last column (21st column).

My GridView contains 20 columns which are added programmatically (DataTable, DataColumn, D开发者_Python百科ataRow 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号