troubleshooting Question

Add checkbox to programmatically generated GridView

Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America asked on
ASP.NET
6 Comments1 Solution1606 ViewsLast Modified:
<Columns>
  <asp:TemplateField>
    <itemtemplate>
      <asp:CheckBox ID="CheckBoxRowItem" runat="server" />
    </itemtemplate>
  </asp:TemplateField>
</Columns>

How do I do this programmatically in the C# code behind?

This checkbox will NOT be databound.


My current method:

private void BuildGridView(DataView dv)
      {            
            this.GridViewSearchResults.Columns.Clear();
            this.GridViewSearchResults.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

/////////////////DOES NOT SEEM TO WORK:
            //CheckBoxField cbf = new CheckBoxField();
            //this.GridViewSearchResults.Columns.Add(cbf);

            foreach (DataColumn dc in dv.Table.Columns)
            {
                  
                  BoundField bf = new BoundField();
                  bf.HeaderText = dc.ColumnName;
                  bf.DataField = dc.ColumnName;
                  bf.HeaderStyle.ForeColor = System.Drawing.Color.White;
                  bf.HeaderStyle.BackColor = System.Drawing.Color.Black;
                  bf.SortExpression = dc.ColumnName;
                  
                  this.GridViewSearchResults.Columns.Add(bf);
            }
      
            this.GridViewSearchResults.AutoGenerateColumns = false;
            this.GridViewSearchResults.DataSource = dv;            
            this.GridViewSearchResults.DataBind();            
      }

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros