Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. cannot add a blank row to a bound datagrid with checkbox control [modified]

cannot add a blank row to a bound datagrid with checkbox control [modified]

Scheduled Pinned Locked Moved ASP.NET
helpquestionsysadmindockerannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BORN again
    wrote on last edited by
    #1

    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!

    T 1 Reply Last reply
    0
    • B BORN again

      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!

      T Offline
      T Offline
      Tuwing Sabado
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • T Tuwing Sabado

        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

        B Offline
        B Offline
        BORN again
        wrote on last edited by
        #3

        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!

        T 1 Reply Last reply
        0
        • B BORN again

          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!

          T Offline
          T Offline
          Tuwing Sabado
          wrote on last edited by
          #4

          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

          B 1 Reply Last reply
          0
          • T Tuwing Sabado

            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

            B Offline
            B Offline
            BORN again
            wrote on last edited by
            #5

            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!

            B 1 Reply Last reply
            0
            • B BORN again

              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!

              B Offline
              B Offline
              BORN again
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups