Gridview having dynamic columns with ObjectDataSource, doesn't persist viewstate on postback
<p>Gridview having dynamic columns with ObjectDataSource, doesn't persist viewstate on postback.</p>

<p>Here are the steps which I follows: - </p>

<p>1) On aspx, I set the GridView DataSourceID to an ObjectDataSource (ObjectContainerDataSource to be specific)</p>

<p>2) ON pageload, I add one dynamic column having checkboxes to GridView along with other collection data. (Not inside !Postback condition).</p>

<p>3) After adding Gridview Columns, I initializes the ObjectDataSource's DataSource property with above Collection.</p>

<p>Now on first time, GridView shows data along with dynamic checkboxes column. </p>

<p>But when I try to find selected checkboxes on button click event, It shows all the checkboxes to be false. (Not persisting checkbox status).</p>

<p>Note : - The above code was working fine without ObjectDataSource. (I just replaced datasource, all code is intact). I guess, its the GridView.DataBind() which caused this.</p>

<p>Code snippet: - </p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
    CreateDynamicGridColumns(ref GridView gridView, ref ObjectContainerDataSource dataObjectSource, List&lt;DataField&gt; DataFields);

    this.dataObjectSource.DataSource = dataSource;
}


public static void CreateDynamicGridColumns(ref GridView gridView, ref ObjectContainerDataSource dataObjectSource, List&lt;DataField&gt; DataFields)
{
            foreach (DataField oField in DataFields)
            {
                TemplateField tf = new TemplateField();
                // create the data rows
                tf.ItemTemplate =
                    new GridViewTemplate(DataControlRowType.DataRow, resources, oField, HostPage);

                // create the header
                tf.HeaderTemplate =
                    new GridViewTemplate(DataControlRowType.Header, resources, oField, HostPage);
                // add to the GridView
                tf.EditItemTemplate =
                    new GridViewEditTemplate(DataControlRowType.DataRow, resources, oField, HostPage);

                gridView.Columns.Add(tf);
            }

            dataObjectSource.DataSource = datasource;
}
</code></pre>

<p>Please provide some help. Thanks in advance...</p>
