Help me with weird datagrid checkbox problem
-
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")
-
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")
try treating cells in that column as bools
If row.item("deleteCheck") then Delete End if
-
try treating cells in that column as bools
If row.item("deleteCheck") then Delete End if
-
Hi! Thanks for the help. But I'm really not sure what you mean. Could you elaborate? Thanks!
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.
-
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.
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 =/
-
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 =/
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 columnDim 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 deletesFor 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 -
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 columnDim 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 deletesFor 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 youI'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!
-
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")
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
-
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
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 =/
-
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 =/
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
-
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!
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