c# - How to bind CheckBoxes from GridView to source DataTable? -
i have create form, in users can create multiple tests data. if user want validate data, must check checkbox in appropiate row , column test , then, click button titled "validate tests". if user want add new test (i.e. test2), must press "add test" button.
all data stored datatable (dttests) , displaying gridview (dgvtests) on aspx page.
my question is, how can checked-values each checkboxes ?
example website: in example, user add 2 tests (look on image: http://i.stack.imgur.com/cr8l5.jpg).
- in test1, want validate years: 1990, 1993, 1999
- in test2, want validate years: 1990, 1993, 1998, 1999
new test column in dttests:
datacolumn dc = new datacolumn("test" + i.tostring()) { datatype = typeof(boolean), readonly = false }; dttests.column.add(dc);
i add new testx column gridview in way:
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);
here's class checkboxtemplate:
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("<b>{0}</b>", 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()); } }
here how can go it, first assign id checkboxes cbx.id = "cbxdotest
.
then add rowcommand
event handler triggered when validate tests button clicked. here need traverse e.row.cells
properties, if cell belonged test column search on checkbox control (checkbox cbx = e.row.cell[i].findcontrol("cbxdotest");
check if cbx.checked
true or false.
i wrote flow , how head double check method/property names.
Comments
Post a Comment