Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Desparate 4 help with checkboxes in datagrid

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
I have a datagrid that has a column for name,StartTime, EndTime, Comments, and Description. The StartTime and EndTime columns are checkboxes. A) I need a function that grabs each checkbox in each row. B) The checkboxes are not yes/no or true/false - they are timestamps. For example, when someone checks the StartTime chkbox for a row, i want it to send back the time they checked it to the database.

Does anyone know how this could be done? Examples are very much appreciated. Thanks
 
My code looks like this, can anyone tell me what im doing wrong that it doesnt want to check the current value of the checkbox.

Dim myDataGridItem As DataGridItem
Dim chkStarted, chkFinished As System.Web.UI.WebControls.CheckBox
Dim taskName, taskName1 As String
Dim TaskID, Name As System.Web.UI.WebControls.Label

For Each myDataGridItem In DataGrid1.Items
chkStarted = DirectCast(myDataGridItem.FindControl("chkStart"), CheckBox)
chkFinished = DirectCast(myDataGridItem.FindControl("chkComplete"), CheckBox)
Session("TaskID") = DirectCast(myDataGridItem.FindControl("TaskID"), Label).Text
Dim dataset As DataSet = LoadTaskByID()
If dataset.Tables("TASKING_LOG").Rows(0).Item("IsStarted") = "False" Then
If chkStarted.Checked Then
Dim Start As DateTime
Dim TaskNum As String
TaskNum = Session("TaskID")
Start = Date.Now.ToShortTimeString
End If
End If
If dataset.Tables("TASKING_LOG").Rows(0).Item("IsEnded") = "False" Then
If chkFinished.Checked Then
Dim EndTime As DateTime
Dim TaskNum As String
TaskNum = Session("TaskID")
EndTime = Date.Now.ToShortTimeString
End If
End If
Next
 
Here's another question: Will the fact that the checkboxes are bringing in a value from a database affect the way i get im trying to do this? The checkboxes are bringing in a true and false value.
 
ac: I am curious as to how you are "triggering" the event? On first glance I would think that you would need to apply java for client triggering, or "edit grid" at the server?
 
i am triggering this by the push of a button outside the datagrid. The problem is that it does not read whether or not that value of the checkbox has changed. It only reads the value of the checkbox once the page is loaded.
 
The DataGrid disconnects with the DataSet/Reader/whatever once all of the info is sent to the page (so that the server can delete objects and free up memory). You can send it back via a viewstate or something, but that involves alot of bandwidth overhead.

You'd probably be best off triggering DataGrid events, like Isadore suggests.
 
It still does not want to grab the current value of the checkbox. It only holds the value of the field from the database. Can you show me how you would grab the current value of the checkbox & not the value that was brought into the datagrid? Thanks i would greatly appreciate it.
 
ac: My first shot would be to add "Edit" -- "Cancel/Submit" pushbuttons to the grid and use an update query to send the results back to the table. Is that what you are doing?
 
I tried doing it that way but im getting the same problem that ive been having every other way. If a checkbox came into the datagrid as false, that's the value it grabs no matter if i changed it or not. What do you suggest?
 
ac: post your code in such a way that we can substitute a simple table and test it. Its a great question, and well worth the time to check it out.

No luck searching other .NET forums for insights I take it?
 
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = CInt(e.Item.ItemIndex)
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim myDataGridItem As DataGridItem
Dim chkStarted, chkFinished As System.Web.UI.WebControls.CheckBox

For Each myDataGridItem In DataGrid1.Items
chkStarted = myDataGridItem.FindControl("chkStart")
chkFinished = myDataGridItem.FindControl("chkComplete")
Session("TaskID") = DirectCast(myDataGridItem.FindControl("TaskID"), Label).Text
Dim dataset As DataSet = LoadTaskByID()

If dataset.Tables("TASKING_LOG").Rows(0).Item("IsStarted") = "False" Then
If chkStarted.Checked = True Then
Dim Start As DateTime
Dim TaskNum As String
TaskNum = Session("TaskID")
Start = Date.Now.ToShortTimeString
Dim ChkConnection As New SqlConnection("Data Source=hqsqldev01;Initial Catalog=production_sched;Integrated Security = SSPI")
Dim UpdateCommand As New SqlCommand("UPDATE dbSyndicate.TASKING_LOG SET StartTime = '" + Start + "', IsStarted = 'Y' WHERE TaskID = '" + TaskNum + "'", ChkConnection)
ChkConnection.Open()
UpdateCommand.ExecuteNonQuery()
ChkConnection.Close()
End If
End If
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top