ASP.NET Two-Way Binding with GridView and Dynamic Templates
<p>I'm trying to get two-way binding working with a simple GridView with Dynamic Template columns.  The code for the Template is as follows:</p>

<pre><code>public class GridViewCheckboxTemplate : ITemplate
{
    private DataControlRowType dcrType;
    private string strColumnName;
    private string strDataType;

    public GridViewCheckboxTemplate(DataControlRowType dcrType, string strColumnName, string strDataType)
    {
        this.dcrType = dcrType;
        this.strColumnName = strColumnName;
        this.strDataType = strDataType;
    }

    public void InstantiateIn(System.Web.UI.Control cContainer)
    {
        DataControlFieldCell dcfCell = null;

        switch(dcrType)
        {
            case DataControlRowType.Header:
                cContainer.Controls.Add(new Literal()
                {
                    Text ="&lt;b&gt;" + strColumnName + "&lt;/b&gt;"
                });
                break;
            case DataControlRowType.DataRow:

                switch(strDataType)
                {
                    case "String":
                        Label lData = new Label();
                        lData.DataBinding += new EventHandler(lData_DataBinding);
                        cContainer.Controls.Add(lData);
                        break;
                    case "Boolean":
                        CheckBox cbData = new CheckBox();
                        cbData.DataBinding += new EventHandler(cbData_DataBinding);
                        cContainer.Controls.Add(cbData);
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void lData_DataBinding(object sender, EventArgs e)
    {
        Label lData = (Label)sender;
        GridViewRow gvRow = (GridViewRow)lData.NamingContainer;

        lData.Text = DataBinder.Eval(gvRow.DataItem, strColumnName).ToString();
    }

    private void cbData_DataBinding(object sender, EventArgs e)
    {
        CheckBox cbData = (CheckBox)sender;
        GridViewRow gvRow = (GridViewRow)cbData.NamingContainer;

        cbData.Checked = Boolean.TrueString.Equals(DataBinder.Eval(gvRow.DataItem, strColumnName).ToString(), StringComparison.CurrentCultureIgnoreCase);
    }
}
</code></pre>

<p>The code for creating the DataTable is as follows:</p>

<pre><code>DataTable dtData = new DataTable();
        dtData.Columns.Add(new DataColumn("id", typeof(Int32)) { AutoIncrement = true });
        dtData.Columns.Add("name", typeof(string));

        for(int i = 0; i &lt; 5; i++)
        {
            dtData.Columns.Add(new DataColumn("day-" + i, typeof(Boolean)));
        }

        foreach(Person pPerson in PersonManager.Instance.GetList())
        {
            DataRow drRow = dtData.NewRow();

            drRow["name"] = pPerson.Name;
            drRow["day-0"] = true;
            drRow["day-1"] = true;
            drRow["day-2"] = false;
            drRow["day-3"] = false;
            drRow["day-4"] = false;

            dtData.Rows.Add(drRow);
        }

        for(int i = 0; i &lt; dtData.Columns.Count; i++)
        {
            if(dtData.Columns[i].ColumnName != "id")
            {
                TemplateField tfField = new TemplateField();
                // create the data rows
                tfField.ItemTemplate = new GridViewCheckboxTemplate(DataControlRowType.DataRow, dtData.Columns[i].ColumnName, dtData.Columns[i].DataType.Name);
                // create the header
                tfField.HeaderTemplate = new GridViewCheckboxTemplate(DataControlRowType.Header, dtData.Columns[i].ColumnName, dtData.Columns[i].DataType.Name);
                // add to the GridView
                this.gvSessions.Columns.Add(tfField);
            }
        }

        this.gvSessions.DataSource = dtData;
        this.gvSessions.DataBind();
</code></pre>

<p>The GridView is defined in the asp page as:</p>

<pre><code>&lt;asp:GridView ID="gvSessions" AutoGenerateColumns="false" BorderWidth="0" BorderStyle="None" Width="100%" runat="server"&gt;
                    &lt;/asp:GridView&gt;
</code></pre>

<p>This works fine and presents a GridView with a row per Person with 5 checkboxes in the desired state.  The problem is that I need to know about any changes that are made when the user click a submit button.  Is there anyway I can get the changes put back into the DataTable?</p>
