title: Simple databinding to gridview columns
body: <p>I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using <em>just databinding</em>, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.</p>
  
  <p>Snippet of <strong>Search.aspx</strong>:</p>
  
  <pre><code>&lt;myControl:FacultyGridView ID="FacultyGridView1" runat="server" /&gt;
      &lt;asp:Repeater ID="Repeater1" runat="server"&gt;
  &lt;ItemTemplate&gt;
      &lt;asp:CheckBox ID="CheckBox1" runat="server" Text='&lt;%# Eval("HeaderText") %&gt;' Checked='&lt;%# Bind("Visible") %&gt;' AutoPostBack=true/&gt;&lt;/ItemTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Code-behind snippet in <strong>Search.aspx.cs</strong>:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
       Repeater1.DataSource = FacultyGridView1.GridView.Columns;
       Repeater1.DataBind();
  }
  </code></pre>
  
  <p>To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of <strong>FacultyGridView.ascx</strong>:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
      AllowPaging="True" AllowSorting="True" PageSize="25"&gt;
      &lt;PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" /&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" /&gt;
          &lt;asp:TemplateField HeaderText="University" SortExpression="UniversityID"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("University.Name") %&gt;' /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Division"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Repeater ID="Repeater1" runat="server" DataSource='&lt;%# Eval("DivisionMemberships") %&gt;'&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Eval("Division.Name") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:Repeater&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" /&gt;
          &lt;asp:TemplateField HeaderText="Research Type"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Repeater ID="Repeater1" runat="server" DataSource='&lt;%# Eval("ResearchTypeMappings") %&gt;'&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("ResearchType.Name") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:Repeater&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" /&gt;
          &lt;asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
              SortExpression="Website" /&gt;
          &lt;asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" /&gt;
          &lt;asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='&lt;%# Eval("EmailAddress", "mailto:{0}") %&gt;'
                      Text='&lt;%# Eval("EmailAddress") %&gt;'&gt;&lt;/asp:HyperLink&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater <em>only</em> on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.</p>
  
  <p>As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.</p>