cannot add a blank row to a bound datagrid with checkbox control [modified]
-
Hello, I think I am posting this question again but I'm quite frustrated as I haven't found any way around. I have a databound datagrid in which New/Blank rows are added to this datagrid on a button click event. This button is outside the datagrid and not in the footer. The datagrid populates well and I can do the edit/update to the already created records. I recently had to add a new checkbox field to my data table. This chekcbox field is to be included in this datagrid. The chkbox works fine for previously created records but as soon as I hit the 'Add New Row' button I get the following error : Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid. Source Error: asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver") %>' /> My code for adding a new/blank row (on button click event) is as below: RefreshGrid() Dim ds as DataSet = DataGrid.datasource Dim blank as DataRow = ds.tables(0).NewRow() ds.tables(0).Rows.insertAt(blank, ds.tables(0).Rows.Count) DataGrid.EditItemIndex = ds.tables(0).Rows.Count-1 DataGrid.DataBind() and my checkbox template column is as below: asp:templatecolumn headertext="Current Driver?"> itemTemplate> asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver") %>' /> I really am clueless on this one. everything seems fine. Nulls are allowed on this field in my datatable.Please help! Thanks. -- modified at 23:58 Monday 26th February, 2007
Aartee. ...HE is watching Us All!
-
Hello, I think I am posting this question again but I'm quite frustrated as I haven't found any way around. I have a databound datagrid in which New/Blank rows are added to this datagrid on a button click event. This button is outside the datagrid and not in the footer. The datagrid populates well and I can do the edit/update to the already created records. I recently had to add a new checkbox field to my data table. This chekcbox field is to be included in this datagrid. The chkbox works fine for previously created records but as soon as I hit the 'Add New Row' button I get the following error : Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid. Source Error: asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver") %>' /> My code for adding a new/blank row (on button click event) is as below: RefreshGrid() Dim ds as DataSet = DataGrid.datasource Dim blank as DataRow = ds.tables(0).NewRow() ds.tables(0).Rows.insertAt(blank, ds.tables(0).Rows.Count) DataGrid.EditItemIndex = ds.tables(0).Rows.Count-1 DataGrid.DataBind() and my checkbox template column is as below: asp:templatecolumn headertext="Current Driver?"> itemTemplate> asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver") %>' /> I really am clueless on this one. everything seems fine. Nulls are allowed on this field in my datatable.Please help! Thanks. -- modified at 23:58 Monday 26th February, 2007
Aartee. ...HE is watching Us All!
Try to change your itemplate column to this. C# <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver")== System.DBNull ? false : DataBinder.Eval(Container.DataItem,"currentdriver") %>' /> </itemTemplate> VB <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")== System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' /> </itemTemplate> Regards, Mark
-
Try to change your itemplate column to this. C# <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver")== System.DBNull ? false : DataBinder.Eval(Container.DataItem,"currentdriver") %>' /> </itemTemplate> VB <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")== System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' /> </itemTemplate> Regards, Mark
Hello Mark, A big thankyou for your prompt help and your time to read my post. I jumped to the solution and did a copy-paste (VB code), but I get the following error : Compiler Error Message: BC30201: Expression expected. Source Error: asp:templatecolumn headertext="Current Driver?"> itemtemplate> asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")== System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' enabled="false" /> /itemtemplate> what am I doing wrong?
Aartee. ...HE is watching Us All!
-
Hello Mark, A big thankyou for your prompt help and your time to read my post. I jumped to the solution and did a copy-paste (VB code), but I get the following error : Compiler Error Message: BC30201: Expression expected. Source Error: asp:templatecolumn headertext="Current Driver?"> itemtemplate> asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")== System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' enabled="false" /> /itemtemplate> what am I doing wrong?
Aartee. ...HE is watching Us All!
try to replace the logical codition "==" to "=". VB - Old Code <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")**==System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' /> </itemTemplate> VB - New Code <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")=**System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' /> </itemTemplate> Regards, Mark -- modified at 1:38 Tuesday 27th February, 2007
-
try to replace the logical codition "==" to "=". VB - Old Code <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")**==System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' /> </itemTemplate> VB - New Code <itemTemplate> <asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")=**System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>' /> </itemTemplate> Regards, Mark -- modified at 1:38 Tuesday 27th February, 2007
Hi Mark, Thanks once again for your help. I tried to change the code as per your suggestion above but now I am getting error : BC30691: 'DBNull' is a type in 'System' and cannot be used as an expression. any ideas? Thanks.
Aartee. ...HE is watching Us All!
-
Hi Mark, Thanks once again for your help. I tried to change the code as per your suggestion above but now I am getting error : BC30691: 'DBNull' is a type in 'System' and cannot be used as an expression. any ideas? Thanks.
Aartee. ...HE is watching Us All!
Thanks MArk, it has worked finally, you were thinking right, just that I had to use 'Is System.DBNull.Value' in place of '==' or '='. Thanks a ton for your solution, it worked like magic.
Aartee. ...HE is watching Us All!