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. General Programming
  3. Visual Basic
  4. Help me with weird datagrid checkbox problem

Help me with weird datagrid checkbox problem

Scheduled Pinned Locked Moved Visual Basic
helpquestionlearning
11 Posts 3 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.
  • K Offline
    K Offline
    Kenshino
    wrote on last edited by
    #1

    I'm not sure if this is the correct area, forgive me if it is not! I have this datagrid that is called deleteCourseGrid and it has a template column. I put a checkbox inside and its ID is deleteCheck. I also have a confirm button that runs the code below. The problem is, somehow, it does not detect that a box is checked. I tried checking each individual rows and cb.checked is always false. Am I doing something wrong? Thanks for help in advance! Dim connection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim command As New SqlCommand("delete from course where courseID = @courseID", connection) Dim i As Integer = 0 Dim cb As CheckBox Dim courseID As String cb = deleteCourseGrid.Items(i).FindControl("deleteCheck") For i = 0 To deleteCourseGrid.Items.Count - 1 If cb.Checked = True Then courseID = deleteCourseGrid.Items(i).Cells(0).Text command.Parameters.Add("@courseID", courseID) connection.Open() command.ExecuteNonQuery() connection.Close() End If Next Response.Redirect("updated.aspx")

    K K 2 Replies Last reply
    0
    • K Kenshino

      I'm not sure if this is the correct area, forgive me if it is not! I have this datagrid that is called deleteCourseGrid and it has a template column. I put a checkbox inside and its ID is deleteCheck. I also have a confirm button that runs the code below. The problem is, somehow, it does not detect that a box is checked. I tried checking each individual rows and cb.checked is always false. Am I doing something wrong? Thanks for help in advance! Dim connection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim command As New SqlCommand("delete from course where courseID = @courseID", connection) Dim i As Integer = 0 Dim cb As CheckBox Dim courseID As String cb = deleteCourseGrid.Items(i).FindControl("deleteCheck") For i = 0 To deleteCourseGrid.Items.Count - 1 If cb.Checked = True Then courseID = deleteCourseGrid.Items(i).Cells(0).Text command.Parameters.Add("@courseID", courseID) connection.Open() command.ExecuteNonQuery() connection.Close() End If Next Response.Redirect("updated.aspx")

      K Offline
      K Offline
      Kevin Nicol
      wrote on last edited by
      #2

      try treating cells in that column as bools If row.item("deleteCheck") then Delete End if

      K 1 Reply Last reply
      0
      • K Kevin Nicol

        try treating cells in that column as bools If row.item("deleteCheck") then Delete End if

        K Offline
        K Offline
        Kenshino
        wrote on last edited by
        #3

        Hi! Thanks for the help. But I'm really not sure what you mean. Could you elaborate? Thanks!

        K 1 Reply Last reply
        0
        • K Kenshino

          Hi! Thanks for the help. But I'm really not sure what you mean. Could you elaborate? Thanks!

          K Offline
          K Offline
          Kevin Nicol
          wrote on last edited by
          #4

          well your normal textbox data column in the grid, displays all strings and numbers as text. It works the same for boolean columns. A DataGridBoolColumn will display true as checked, false as unchecked. So whatever column on your datatable is mapped to this DataGridBoolColumn will contain the true or false that is shown in the datagrid column. If you had a datatable (Two columns -> Name, Smoker) where smoker would be mapped to the DataGridBoolColumn, in each row in the table, a boolean would appear in the column smoker with resepct to the checked state of the textbox on the datagrid. Hope that helps.

          K 1 Reply Last reply
          0
          • K Kevin Nicol

            well your normal textbox data column in the grid, displays all strings and numbers as text. It works the same for boolean columns. A DataGridBoolColumn will display true as checked, false as unchecked. So whatever column on your datatable is mapped to this DataGridBoolColumn will contain the true or false that is shown in the datagrid column. If you had a datatable (Two columns -> Name, Smoker) where smoker would be mapped to the DataGridBoolColumn, in each row in the table, a boolean would appear in the column smoker with resepct to the checked state of the textbox on the datagrid. Hope that helps.

            K Offline
            K Offline
            Kenshino
            wrote on last edited by
            #5

            Thanks Kevin. I think understand the logic behind your explanation, however I fail to find anything that relates DataGridBoolColumn in Visual Studio 2003. Is there something that I'm missing? What I'm trying to do is to allow the administrator to tick the courses that he wants to delete and then when he clicks the confirm button, the code scrolls through, if it's ticked, it sends the SQL command and deletes it. I'm sorry for troubling you =/

            K 1 Reply Last reply
            0
            • K Kenshino

              Thanks Kevin. I think understand the logic behind your explanation, however I fail to find anything that relates DataGridBoolColumn in Visual Studio 2003. Is there something that I'm missing? What I'm trying to do is to allow the administrator to tick the courses that he wants to delete and then when he clicks the confirm button, the code scrolls through, if it's ticked, it sends the SQL command and deletes it. I'm sorry for troubling you =/

              K Offline
              K Offline
              Kevin Nicol
              wrote on last edited by
              #6

              ok This is how I would do that. Lets say you have a datatable (courseTable) with one column (A course ID). First add a second column to the table courseTable.columns.add(new dataColumn("Delete")) Setup the tablestyle for the bool column Dim style As New DataGridTableStyle style.GridColumnStyles.Clear() style.MappingName = courseTable.TableName Dim col As New DataGridBoolColumn col.MappingName = "Delete" col.HeaderText = "Delete" col.AllowNull = False style.GridColumnStyles.Add(col) DataGrid1.TableStyles.Add(style) DataGrid1.DataSource = courseTable now you have the right setup for the bool column, just display it by adding rows to the datatable somehow from the database. so in your confirm button routine just check for deletes For Each row As DataRow In courseTable.Rows If row.Item("Delete") Then 'This row was ticked so perform a delete End If Next That should do it for you

              K 1 Reply Last reply
              0
              • K Kevin Nicol

                ok This is how I would do that. Lets say you have a datatable (courseTable) with one column (A course ID). First add a second column to the table courseTable.columns.add(new dataColumn("Delete")) Setup the tablestyle for the bool column Dim style As New DataGridTableStyle style.GridColumnStyles.Clear() style.MappingName = courseTable.TableName Dim col As New DataGridBoolColumn col.MappingName = "Delete" col.HeaderText = "Delete" col.AllowNull = False style.GridColumnStyles.Add(col) DataGrid1.TableStyles.Add(style) DataGrid1.DataSource = courseTable now you have the right setup for the bool column, just display it by adding rows to the datatable somehow from the database. so in your confirm button routine just check for deletes For Each row As DataRow In courseTable.Rows If row.Item("Delete") Then 'This row was ticked so perform a delete End If Next That should do it for you

                K Offline
                K Offline
                Kenshino
                wrote on last edited by
                #7

                I'm trying this out and I have a strong feeling that it will help with my problem. However, I am stuck at trying to dim a new DataGridTableStyle. Is this not available in The .Net 1.1 framework or something? Visual Studio 2003 says that Type DataGridTableStyle is not defined. I tried reading http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx#S6 and it talks about DataGridTableStyle, yet visual studio does not seem to recognise this. What should I do? And a million thanks for your many replies!

                K 1 Reply Last reply
                0
                • K Kenshino

                  I'm not sure if this is the correct area, forgive me if it is not! I have this datagrid that is called deleteCourseGrid and it has a template column. I put a checkbox inside and its ID is deleteCheck. I also have a confirm button that runs the code below. The problem is, somehow, it does not detect that a box is checked. I tried checking each individual rows and cb.checked is always false. Am I doing something wrong? Thanks for help in advance! Dim connection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim command As New SqlCommand("delete from course where courseID = @courseID", connection) Dim i As Integer = 0 Dim cb As CheckBox Dim courseID As String cb = deleteCourseGrid.Items(i).FindControl("deleteCheck") For i = 0 To deleteCourseGrid.Items.Count - 1 If cb.Checked = True Then courseID = deleteCourseGrid.Items(i).Cells(0).Text command.Parameters.Add("@courseID", courseID) connection.Open() command.ExecuteNonQuery() connection.Close() End If Next Response.Redirect("updated.aspx")

                  K Offline
                  K Offline
                  Kapil Thakur
                  wrote on last edited by
                  #8

                  hi, you are currently taking the checked value as string. Instead of it, take it as a boolean variable. i m using your code and the changes are reflected in BOLD text. Dim cb As BOOLEAN Dim courseID As boolean cb = CTYPE(deleteCourseGrid.Items(i).FindControl("deleteCheck"), CHECKBOX).CHECKED For i = 0 To deleteCourseGrid.Items.Count - 1 IF cb = TRUE THEN courseID = deleteCourseGrid.Items(i).Cells(0).Text command.Parameters.Add("@courseID", courseID) connection.Open() command.ExecuteNonQuery() connection.Close() End If use this and your code will work fine. ALSO check that you are reloading the datagrid on page_load Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com

                  K 1 Reply Last reply
                  0
                  • K Kapil Thakur

                    hi, you are currently taking the checked value as string. Instead of it, take it as a boolean variable. i m using your code and the changes are reflected in BOLD text. Dim cb As BOOLEAN Dim courseID As boolean cb = CTYPE(deleteCourseGrid.Items(i).FindControl("deleteCheck"), CHECKBOX).CHECKED For i = 0 To deleteCourseGrid.Items.Count - 1 IF cb = TRUE THEN courseID = deleteCourseGrid.Items(i).Cells(0).Text command.Parameters.Add("@courseID", courseID) connection.Open() command.ExecuteNonQuery() connection.Close() End If use this and your code will work fine. ALSO check that you are reloading the datagrid on page_load Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com

                    K Offline
                    K Offline
                    Kenshino
                    wrote on last edited by
                    #9

                    Hi! Thanks for the help!! I tried your way, however it does not work. I've been stepping the program through and the problem lies with the fact that it is somehow ignoring boxes that are checked. So it keeps looping to If cb = true then and then skipping the entire code within and just stepping over to End If This is really confusing =/

                    K 1 Reply Last reply
                    0
                    • K Kenshino

                      Hi! Thanks for the help!! I tried your way, however it does not work. I've been stepping the program through and the problem lies with the fact that it is somehow ignoring boxes that are checked. So it keeps looping to If cb = true then and then skipping the entire code within and just stepping over to End If This is really confusing =/

                      K Offline
                      K Offline
                      Kapil Thakur
                      wrote on last edited by
                      #10

                      hi, first of all make sure that before checking the checkboxes values....you are not reloading the datagrid with data again. make sure that after clicking the button you are loading the data again at some point in your code.....as this is the most common mistake in such scenarios. so, be sure that the grid is not reloaded with data. anyways....... i m attaching a thoroughly tested and working code snippet. Dim count As Integer = 0 For count = 0 To dgUser.Items.Count - 1 Dim IsChecked As String = CType(dgUser.Items(count).FindControl("chkDeleteUser"), CheckBox).Checked.ToString() If UCase(Trim(IsChecked)) = UCase(Trim("True")) Then ''IGNORE THE FOLLOWING If-EndIf block - starting here 'If UCase(Trim(Session("userid"))) = UCase(Trim(dgUser.Items(count).Cells(1).Text.ToString)) Then 'ErrorLabel.Visible = True 'ErrorLabel.Text = Session("oRM").GetString("UD_You_Are_Not_Allowed_To_Delete_Your_Own_Data") & " - " & dgUser.Items(count).Cells(3).Text.ToString 'pnlGrid.Visible = True 'pnlAddUser.Visible = False 'Exit Sub 'End If ''IGNORE THE FOLLOWING If-EndIf block - ending here End If Next if even this is not working......then my dear frnd , send ur project to me...i'll be glad to help you out by debuging it and fixing your problem my email id is thakur.kapil@gmail.com Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com

                      1 Reply Last reply
                      0
                      • K Kenshino

                        I'm trying this out and I have a strong feeling that it will help with my problem. However, I am stuck at trying to dim a new DataGridTableStyle. Is this not available in The .Net 1.1 framework or something? Visual Studio 2003 says that Type DataGridTableStyle is not defined. I tried reading http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx#S6 and it talks about DataGridTableStyle, yet visual studio does not seem to recognise this. What should I do? And a million thanks for your many replies!

                        K Offline
                        K Offline
                        Kapil Thakur
                        wrote on last edited by
                        #11

                        DataGridTableStyle is valid in windows application. but you are working in web applications that why you are getting it and it is giving errors. anyways next time post your questions in asp.net forum....you have currently posted this question in vb.net ..............thats why the person has misunderstood the scenario and he has given this answer.....anyways it happens.. hope you have found solution to your problem. Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com

                        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