c# - Checkbox refused to bind data -


when data loaded gridview, boolean values should show in checkboxes. checkbox should in editable format.

i getting number of errors: invalid cast, string not in valid format etc.

i searched in google , here well. there number of devs faced same or similar issues , have managed solved.

mon_s1 boolean (true, false). changed string test out following code snippets.

checked='<%# bool.parse(eval("mon_s1").tostring()) %>'

checked='<%# eval("mon_s1") %>'

checked='<%# eval("mon_s1").tostring().equals("1") %>' checked='<%# bind("mon_s1").tostring().equals("1") %>'

but no matter combination of answers tried, it's not displaying data per db infact has boolean values.

stacktrace following cast error:

line 143:        <asp:templatefield headertext="ms1"> line 144:            <itemtemplate> line 145:                <asp:checkbox id="cbms1" runat="server" checked='<%# eval("mon_s1") %>'/> line 146:            </itemtemplate> line 147:        </asp:templatefield> 

stack trace:

[invalidcastexception: specified cast not valid.]    asp.testschedule_aspx.__databinding__control38(object sender, eventargs e) in c:\test - 0509 working\testschedule.aspx:145    system.web.ui.control.ondatabinding(eventargs e) +92    system.web.ui.control.databind(boolean raiseondatabinding) +85    system.web.ui.control.databind() +15    system.web.ui.control.databindchildren() +187    system.web.ui.control.databind(boolean raiseondatabinding) +94    system.web.ui.control.databind() +15    system.web.ui.control.databindchildren() +187    system.web.ui.control.databind(boolean raiseondatabinding) +94    system.web.ui.control.databind() +15    system.web.ui.webcontrols.gridview.createrow(int32 rowindex, int32 datasourceindex, datacontrolrowtype rowtype, datacontrolrowstate rowstate, boolean databind, object dataitem, datacontrolfield[] fields, tablerowcollection rows, pageddatasource pageddatasource) +167    system.web.ui.webcontrols.gridview.createchildcontrols(ienumerable datasource, boolean databinding) +3724    system.web.ui.webcontrols.compositedataboundcontrol.performdatabinding(ienumerable data) +67    system.web.ui.webcontrols.gridview.performdatabinding(ienumerable data) +14    system.web.ui.webcontrols.databoundcontrol.ondatasourceviewselectcallback(ienumerable data) +123    system.web.ui.datasourceview.select(datasourceselectarguments arguments, datasourceviewselectcallback callback) +33    system.web.ui.webcontrols.databoundcontrol.performselect() +138    system.web.ui.webcontrols.basedataboundcontrol.databind() +30    system.web.ui.webcontrols.gridview.databind() +4    testschedule.testschedule.bindgridviewdataschedule() in c:\test - 0509 working\testschedule.aspx:39    testschedule.testschedule.page_load(object sender, eventargs e) in c:\test - 0509 working\testschedule.aspx.cs:22    system.web.util.callieventhandlerdelegateproxy.callback(object sender, eventargs e) +51    system.web.ui.control.onload(eventargs e) +92    system.web.ui.control.loadrecursive() +54    system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +772 

enter image description here

instead of keep struggling aspx , eval approaches finished using codebehind, use gridview's rowdatabound-event:

protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) {     if (e.row.rowtype == datacontrolrowtype.datarow)     {         checkbox chk = (checkbox) e.row.findcontrol("cbms1");         datarow row = ((datarowview)e.row.dataitem).row;         chk.checked = row.field<bool>("mon_s1");     } } 

update: since seems nullable according comment, use this:

bool? nullortrue = row.field<bool?>("mon_s1"); chk.checked = nullortrue.hasvalue && nullortrue.value; 

if it's not bool string change row.field<bool> row.field<string>. benefit of codebehind have compile time safety , debugging capability.

so if gives exception can fix easily. need set breakpoint @ first line , expect values in debugger's quick watch window. there can find out types involved. maybe e.row.dataitem not datarowview, @ in debugger , use correct type.

i prefer not use frontend business logic for other reasons.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -