Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind CheckBoxes from GridView to source DataTable?
    primarykey
    data
    text
    <p>I have to create some form, in which users can create multiple tests for some data. If user want to validate data, he must check Checkbox in appropiate row and column for a test and then, click button titled "Validate tests". If user want to add new test (i.e. Test2), he must press "Add test" button.</p> <p>All data are stored into DataTable (dtTests) and displaying by GridView (dgvTests) on aspx page.</p> <p>My question is, how can i get checked-values for each checkboxes ?</p> <p><strong>Example website:</strong> In Example, user add two tests (look on image: <a href="http://i.stack.imgur.com/cR8l5.jpg" rel="nofollow">http://i.stack.imgur.com/cR8l5.jpg</a>). </p> <ul> <li>In Test1, he want to validate years: 1990, 1993, 1999 </li> <li>In Test2, he want to validate years: 1990, 1993, 1998, 1999</li> </ul> <p><strong>New Test column in dtTests:</strong></p> <pre><code>DataColumn dc = new DataColumn("Test" + i.ToString()) { DataType = typeof(Boolean), ReadOnly = false }; dtTests.Column.Add(dc); </code></pre> <p><strong>I add new TestX column to gridView in this way:</strong></p> <pre><code>TemplateField cbxTest = new TemplateField(); cbxTest.HeaderTemplate = new CheckBoxTemplate(DataControlRowType.Header, "Test" + i.ToString(), "Test" + i.ToString()); cbxTest.ItemTemplate = new CheckBoxTemplate(DataControlRowType.DataRow, "Test" + i.ToString(), "Test" + i.ToString()); dgvTests.Columns.Add(cbxTest); </code></pre> <p><strong>Here's class for CheckBoxTemplate:</strong></p> <pre><code>public class CheckBoxTemplate : ITemplate { private DataControlRowType templateType; private string columnName; private string bindName; public CheckBoxTemplate(DataControlRowType _type, string _colName, string _bindName) { templateType = _type; columnName = _colName; bindName = _bindName; } public void InstantiateIn(Control container) { switch (templateType) { case DataControlRowType.Header: Literal lc = new Literal() { Text = String.Format("&lt;b&gt;{0}&lt;/b&gt;", columnName) }; container.Controls.Add(lc); break; case DataControlRowType.DataRow: CheckBox cbx = new CheckBox(); cbx.DataBinding += new EventHandler(this.cbx_DataBinding); container.Controls.Add(cbx); break; default: break; } } private void cbx_DataBinding(Object sender, EventArgs e) { CheckBox cbx = (CheckBox)sender; GridViewRow row = (GridViewRow)cbx.NamingContainer; cbx.Checked = Convert.ToBoolean(DataBinder.Eval(row.DataItem, bindName).ToString()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload