robtyketto
Member
- Joined
- Jul 23, 2009
- Messages
- 23
- Programming Experience
- Beginner
Greetings,
I have a datagridview which is created progmatically and all but two of the columns (both DataGridViewCheckBoxColumns) are bound columns to a datatable.
One of the checkboxcolumns is used to select row(s) for the purpose of deletion, the code to delete the rows, removes the datarows from the database and the datagridview source datatable (See code below).
The issue I'm having is where I remove a datarow and accept the datatable changes the unbound "select" column has all it's values reset to "false" when the datagridview refreshes.
One idea I had was to was to include the select column in the datatable as boolean, but as SQLServer database backend I believe it may be difficult to store as boolean datatypes don't exist.
Does anyone have some possible solutions to this problem?
Thanks
Rob
I have a datagridview which is created progmatically and all but two of the columns (both DataGridViewCheckBoxColumns) are bound columns to a datatable.
One of the checkboxcolumns is used to select row(s) for the purpose of deletion, the code to delete the rows, removes the datarows from the database and the datagridview source datatable (See code below).
VB.NET:
'Loop around each row in datagridview
'For Each booking As DataGridViewRow In DataGridView1.Rows
For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
Dim num As Integer = (From bookings As DataGridViewRow In DataGridView1.Rows _
Select bookings _
Where bookings.Cells("selectColumn").Value = "True").Count
'Get bookings date and endtime to sue to check if booking is in past, present or future
Dim bookingStartDateTime As New Date(DirectCast(DataGridView1.Rows(i).Cells("bookDate").Value, Date).Year, _
DirectCast(DataGridView1.Rows(i).Cells("bookDate").Value, Date).Month, _
DirectCast(DataGridView1.Rows(i).Cells("bookDate").Value, Date).Day, _
DirectCast(DataGridView1.Rows(i).Cells("startTime").Value, DateTime).Hour, _
DirectCast(DataGridView1.Rows(i).Cells("startTime").Value, DateTime).Minute, 0)
'Is cell value of selected is TRUE and name the same as login
If (DataGridView1.Rows(i).Cells("name").Value.Equals(bookersName) AndAlso DataGridView1.Rows(i).Cells("selectColumn").Value = "True" _
AndAlso Date.Now < bookingStartDateTime) Then
'Remove from database
Dim deleteBooking As New bookingsSession
If deleteBooking.bookingDeletion(cboRoom.SelectedValue, DataGridView1.Rows(i).Cells("bookDate").Value, _
DataGridView1.Rows(i).Cells("startTime").Value, DataGridView1.Rows(i).Cells("endTime").Value) = True Then
'Remove booking from datagridview via datatable
bookingsdata.Tables("bookings").Rows.Remove(DirectCast(DataGridView1.Rows(i).DataBoundItem, DataRowView).Row)
bookingsdata.Tables("bookings").AcceptChanges()
End If
ElseIf (Not DataGridView1.Rows(i).Cells("name").Value.Equals(bookersName) AndAlso DataGridView1.Rows(i).Cells("selectColumn").Value = "True") Then
'Highlight which bookings cannot be removed as not made by the user
MessageBox.Show("Warning: You are unable to delete the booking made on " _
& "the " & DirectCast(DataGridView1.Rows(i).Cells("bookDate").Value, DateTime).Date _
& " at " & CType(DataGridView1.Rows(i).Cells("starttime").Value, DateTime).ToString("HH:mm") _
& " to " & CType(DataGridView1.Rows(i).Cells("endtime").Value, DateTime).ToString("HH:mm") _
& " as booking was made by someone else.", "Warning: Unable to cancel booking", _
MessageBoxButtons.OK)
DataGridView1.Rows(i).Cells("selectColumn").Value = "False"
ElseIf (DataGridView1.Rows(i).Cells("name").Value.Equals(bookersName) AndAlso DataGridView1.Rows(i).Cells("selectColumn").Value = "True" _
AndAlso Date.Now > bookingStartDateTime) Then
MessageBox.Show("Cannot delete a booking for one in the past")
End If
Next 'Retrieve next datgridviewrow for expection
The issue I'm having is where I remove a datarow and accept the datatable changes the unbound "select" column has all it's values reset to "false" when the datagridview refreshes.
One idea I had was to was to include the select column in the datatable as boolean, but as SQLServer database backend I believe it may be difficult to store as boolean datatypes don't exist.
Does anyone have some possible solutions to this problem?
Thanks
Rob