Datagrid Edit, Insert & Update Checkbox (ASP.NET Using VB.NET)

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Datagrid Edit, Insert & Update Checkbox (ASP.NET Using VB.NET)

    Hi you guys. If you could possible help me here.
    I am quite new to .NET coming over from VB and with limited ASP
    development. I am having a problem with my checkbox in the fact that if I
    click edit on my datagrid the checkbox does not receive the value from the
    Database. I have tried several ways in doing this but to no avail.

    -----------------------
    Here is the code I use:
    -----------------------

    Select Case e.CommandName
    Case "Insert"
    If Me.masterDataGrid.EditItemIndex >= 0 Then
    lblError.Text = "Cannot Insert While Editing Another Record"
    Exit Sub
    End If
    lblError.Text = ""
    ' Add data to the row.
    ' Add the new row to the table.
    Me.DS_Profiles1.Tables(0).NewRow.Item("Name") = ""
    Me.DS_Profiles1.Tables(0).NewRow.Item("Phone") = ""
    Me.DS_Profiles1.Tables(0).NewRow.Item("Cell") = ""
    Me.DS_Profiles1.Tables(0).NewRow.Item("Email") = ""
    Me.DS_Profiles1.Tables(0).NewRow.Item("IDnumber") = ""
    Me.DS_Profiles1.Tables(0).NewRow.Item("LastMedical ") = Date.Now
    Me.DS_Profiles1.Tables(0).NewRow.Item("Active") = True
    Dim rowNew As System.Data.DataRow = Me.DS_Profiles1.Tables(0).NewRow
    Me.DS_Profiles1.Tables(0).Rows.Add(rowNew)
    Me.masterDataGrid.SelectedIndex = Me.masterDataGrid.Items.Count
    Me.masterDataGrid.EditItemIndex = Me.masterDataGrid.Items.Count
    Me.masterDataGrid.DataBind()
    Session("ssDSProfile") = DS_Profiles1
    Case "Edit"
    lblError.Text = ""
    Dim findval As Integer
    Dim checkval As String
    checkval = masterDataGrid.Items(masterDataGrid.SelectedIndex) .Cells
    (7).Text
    Me.masterDataGrid.EditItemIndex = e.Item.ItemIndex
    Me.masterDataGrid.DataBind()
    Dim cb As CheckBox = CType(masterDataGrid.Items
    (masterDataGrid.EditItemIndex).FindControl("cbActi ve"), CheckBox)
    Response.Write(checkval)
    Response.End()
    If checkval = "True" Then
    cb.Checked = True
    Else
    cb.Checked = False
    End If

    Case "Update"
    With masterDataGrid
    Dim chkbox As New CheckBox
    chkbox = CType(.Items(.EditItemIndex).FindControl("cbActive "),
    CheckBox)

    If chkbox.Checked = True Then
    CType(.Items(.EditItemIndex).Cells(7).Controls(1), CheckBox)
    ..Checked = True
    Else
    CType(.Items(.EditItemIndex).Cells(7).Controls(1), CheckBox)
    ..Checked = False
    End If ' set checkbox avtive
    Dim intcount As Integer
    For intcount = 1 To .Items(.EditItemIndex).Cells.Count
    If intcount = .Items(.EditItemIndex).Cells.Count Then
    Exit For
    End If
    '///////////////////////////////////////////////////////////////////////
    '/// Check that control exists in this position
    '///////////////////////////////////////////////////////////////////////
    If .Items(.EditItemIndex).Cells(intcount).Controls.Co unt = 1 Then
    If TypeOf (.Items(.EditItemIndex). _
    Cells(intcount). _
    Controls(0)) _
    Is TextBox Then
    If CType(.Items(.EditItemIndex).Cells(intcount)
    ..Controls(0), _
    TextBox).Text = "" Then 'insert NULL if no data
    If intcount = 2 Or intcount = 4 Then
    lblError.Text = "FILL IN ALL REQUIRED
    FIELDS"
    Exit Sub
    End If

    Else ' if not ""
    Me.DS_Profiles1.Tables(0).Rows(.EditItemIndex)
    ..Item(.Columns(intcount).SortExpression) = _
    CType(.Items(.EditItemIndex).Cells(intcount). _
    Controls(0), TextBox).Text()
    End If
    End If
    End If
    If .Items(.EditItemIndex).Cells(intcount).Controls.Co unt =
    3 Then
    If TypeOf (.Items(.EditItemIndex). _
    Cells(intcount). _
    Controls(1)) _
    Is TextBox Then
    If CType(.Items(.EditItemIndex).Cells(intcount)
    ..Controls(1), _
    TextBox).Text = "" Then 'insert NULL if no data
    If intcount = 2 Or intcount = 4 Then
    lblError.Text = "FILL IN ALL REQUIRED
    FIELDS"
    Exit Sub
    End If
    Else ' if not ""
    Me.DS_Profiles1.Tables(0).Rows(.EditItemIndex)
    ..Item(.Columns(intcount).SortExpression) = _
    CType(.Items(.EditItemIndex).Cells(intcount). _
    Controls(1), TextBox).Text()
    End If
    End If
    End If
    Next
    With Me.AquaDB
    Try
    .objDA_Profiles.Update(Me.DS_Profiles1)
    Catch myerror As System.Data.NoNullAllowedException
    Me.lblError.Text = "Could Not Update!"
    End Try
    End With
    .SelectedIndex = -1
    .EditItemIndex = -1
    Me.Session("ssDSProfile") = Me.DS_Profiles1
    .DataBind()
    End With
    Case "Cancel"
    lblError.Text = ""
    Me.masterDataGrid.SelectedIndex = -1
    Me.masterDataGrid.EditItemIndex = -1
    Me.DS_Profiles1.RejectChanges()
    Me.Session("ssDSProfile") = Me.DS_Profiles1
    Me.masterDataGrid.DataBind()
    End Select

    --
    Message posted via [url]http://www.dotnetmonster.com[/url]
    Jacobus Visser via DotNetMonster.com Guest

  2. Similar Questions and Discussions

    1. datagrid checkbox list edit item not working
      I am using asp template columns. I display box values from a lookup table. The datagrid displays field "bookid" and the checkbox displays...
    2. Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.
      I am trying to update the database field when the checkbox is clicked. I am trying to modified the following solution but.. got stuck on the...
    3. Datagrid checkbox value update issue
      Hi, All, I am trying to bulid a web application for user's to approve certain information. On the web page, it shows a datagrid containing the...
    4. To all Gurus: How can I edit/update a DataGrid in a DataGrid (nested DataGrid)? Possible?
      Hello, I am searching the whole Internet for a good example how to edit/update a DataGrid in a DataGrid (nested DataGrid). I know how to...
  3. #2

    Default Datagrid Edit, Insert & Update Checkbox (ASP.NET Using VB.NET)

    Hi

    Don't use Response.End() before you finish all execution
    code. Response.End() means stop server execution and send
    response content to client immediately. Hence any code
    after it will nerver be executed.

    HTH

    Elton Wang
    [email]elton_wang@hotmail.com[/email]
    >-----Original Message-----
    >Hi you guys. If you could possible help me here.
    >I am quite new to .NET coming over from VB and with
    limited ASP
    >development. I am having a problem with my checkbox in
    the fact that if I
    >click edit on my datagrid the checkbox does not receive
    the value from the
    >Database. I have tried several ways in doing this but to
    no avail.
    >
    >-----------------------
    >Here is the code I use:
    >-----------------------
    >
    >Select Case e.CommandName
    > Case "Insert"
    > If Me.masterDataGrid.EditItemIndex >= 0 Then
    > lblError.Text = "Cannot Insert While Editing
    Another Record"
    > Exit Sub
    > End If
    > lblError.Text = ""
    > ' Add data to the row.
    > ' Add the new row to the table.
    > Me.DS_Profiles1.Tables(0).NewRow.Item("Name") = ""
    > Me.DS_Profiles1.Tables(0).NewRow.Item("Phone")
    = ""
    > Me.DS_Profiles1.Tables(0).NewRow.Item("Cell") = ""
    > Me.DS_Profiles1.Tables(0).NewRow.Item("Email")
    = ""
    > Me.DS_Profiles1.Tables(0).NewRow.Item("IDnumber")
    = ""
    > Me.DS_Profiles1.Tables(0).NewRow.Item
    ("LastMedical") = Date.Now
    > Me.DS_Profiles1.Tables(0).NewRow.Item("Active") =
    True
    > Dim rowNew As System.Data.DataRow =
    Me.DS_Profiles1.Tables(0).NewRow
    > Me.DS_Profiles1.Tables(0).Rows.Add(rowNew)
    > Me.masterDataGrid.SelectedIndex =
    Me.masterDataGrid.Items.Count
    > Me.masterDataGrid.EditItemIndex =
    Me.masterDataGrid.Items.Count
    > Me.masterDataGrid.DataBind()
    > Session("ssDSProfile") = DS_Profiles1
    > Case "Edit"
    > lblError.Text = ""
    > Dim findval As Integer
    > Dim checkval As String
    > checkval = masterDataGrid.Items
    (masterDataGrid.SelectedIndex).Cells
    >(7).Text
    > Me.masterDataGrid.EditItemIndex = e.Item.ItemIndex
    > Me.masterDataGrid.DataBind()
    > Dim cb As CheckBox = CType(masterDataGrid.Items
    >(masterDataGrid.EditItemIndex).FindControl("cbAct ive"),
    CheckBox)
    > Response.Write(checkval)
    > Response.End()
    > If checkval = "True" Then
    > cb.Checked = True
    > Else
    > cb.Checked = False
    > End If
    >
    > Case "Update"
    > With masterDataGrid
    > Dim chkbox As New CheckBox
    > chkbox = CType(.Items
    (.EditItemIndex).FindControl("cbActive"),
    >CheckBox)
    >
    > If chkbox.Checked = True Then
    > CType(.Items(.EditItemIndex).Cells
    (7).Controls(1), CheckBox)
    >..Checked = True
    > Else
    > CType(.Items(.EditItemIndex).Cells
    (7).Controls(1), CheckBox)
    >..Checked = False
    > End If ' set checkbox avtive
    > Dim intcount As Integer
    > For intcount = 1 To .Items
    (.EditItemIndex).Cells.Count
    > If intcount = .Items
    (.EditItemIndex).Cells.Count Then
    > Exit For
    > End If
    >'/////////////////////////////////////////////////////////
    //////////////
    >'/// Check that control exists in this position
    >'/////////////////////////////////////////////////////////
    //////////////
    >If .Items(.EditItemIndex).Cells(intcount).Controls.Co unt
    = 1 Then
    > If TypeOf (.Items(.EditItemIndex). _
    > Cells(intcount). _
    > Controls(0)) _
    > Is TextBox Then
    > If CType(.Items
    (.EditItemIndex).Cells(intcount)
    >..Controls(0), _
    > TextBox).Text = ""
    Then 'insert NULL if no data
    > If intcount = 2 Or intcount =
    4 Then
    > lblError.Text = "FILL IN
    ALL REQUIRED
    >FIELDS"
    > Exit Sub
    > End If
    >
    > Else ' if
    not ""
    > Me.DS_Profiles1.Tables(0).Rows
    (.EditItemIndex)
    >..Item(.Columns(intcount).SortExpression) = _
    > CType(.Items
    (.EditItemIndex).Cells(intcount). _
    > Controls(0),
    TextBox).Text()
    > End If
    > End If
    > End If
    > If .Items(.EditItemIndex).Cells
    (intcount).Controls.Count =
    >3 Then
    > If TypeOf (.Items(.EditItemIndex). _
    > Cells(intcount). _
    > Controls(1)) _
    > Is TextBox Then
    > If CType(.Items
    (.EditItemIndex).Cells(intcount)
    >..Controls(1), _
    > TextBox).Text = ""
    Then 'insert NULL if no data
    > If intcount = 2 Or intcount =
    4 Then
    > lblError.Text = "FILL IN
    ALL REQUIRED
    >FIELDS"
    > Exit Sub
    > End If
    > Else ' if
    not ""
    > Me.DS_Profiles1.Tables(0).Rows
    (.EditItemIndex)
    >..Item(.Columns(intcount).SortExpression) = _
    > CType(.Items
    (.EditItemIndex).Cells(intcount). _
    > Controls(1),
    TextBox).Text()
    > End If
    > End If
    > End If
    > Next
    > With Me.AquaDB
    > Try
    > .objDA_Profiles.Update
    (Me.DS_Profiles1)
    > Catch myerror As
    System.Data.NoNullAllowedException
    > Me.lblError.Text = "Could Not Update!"
    > End Try
    > End With
    > .SelectedIndex = -1
    > .EditItemIndex = -1
    > Me.Session("ssDSProfile") = Me.DS_Profiles1
    > .DataBind()
    > End With
    > Case "Cancel"
    > lblError.Text = ""
    > Me.masterDataGrid.SelectedIndex = -1
    > Me.masterDataGrid.EditItemIndex = -1
    > Me.DS_Profiles1.RejectChanges()
    > Me.Session("ssDSProfile") = Me.DS_Profiles1
    > Me.masterDataGrid.DataBind()
    >End Select
    >
    >--
    >Message posted via [url]http://www.dotnetmonster.com[/url]
    >.
    >
    Elton Wang Guest

  4. #3

    Default Re: Datagrid Edit, Insert & Update Checkbox (ASP.NET Using VB.NET)

    Sorry forgot to remove the response code. I just inserted it into the code
    to check what the values was before I posted the previous message.

    --
    Message posted via [url]http://www.dotnetmonster.com[/url]
    Jacobus Visser via DotNetMonster.com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139