|
-
Nov 12th, 2003, 04:21 PM
#1
Thread Starter
Hyperactive Member
Mapping column in currow of datagrid
I have a datagrid where I have successfully been able to get the current row number to match the selected row. I can also delete a row by an ID number.
What I need to do is match the ID number to the ID number in the currow so I can delete it with my Delete From Statement.
Here is my code so far. What do I need to do?
VB Code:
Dim curRow As Integer
curRow = DataGrid1.CurrentRowIndex
MsgBox(curRow)
Dim strDataBaseName As String = "C:\path\dbname.mdb"
Dim cnn As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDataBaseName)
Dim cmd As New OleDb.OleDbCommand
cnn.Open()
cmd.Connection = cnn
Try
cmd.CommandText = "Delete From TimeGr Where ID = " & 33 ' map this
' to the ID in the current row instead of having it hardwired in
' the code.
cmd.ExecuteNonQuery()
Catch exError As Exception
MessageBox.Show(exError.Message)
End Try
-
Nov 12th, 2003, 11:57 PM
#2
Frenzied Member
Imagine the ID column in your grid is the first column for example, then:
VB Code:
Dim ID as Integer
ID=Datagrid1.Item(DataGrid1.CurrentRowIndex,0)
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Nov 13th, 2003, 09:49 AM
#3
Thread Starter
Hyperactive Member
Thank you Lunatic3 that did the trick One more question. Is there a built in command to refresh the datagrid after the delete to relect the change?
I am Googling it now but wanted the question out there just in case I don't find anything.
Thanks again!
Last edited by BukHix; Nov 13th, 2003 at 09:55 AM.
-
Nov 13th, 2003, 05:48 PM
#4
Frenzied Member
To refresh the data in datagridgrid you have to refresh the underlying datasource. There is a Refresh method in the Datagrid (available for generic control object) that just refreshes the control, i.e invalidates and repaints it. So Don't confuse these two.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Nov 14th, 2003, 09:06 AM
#5
Thread Starter
Hyperactive Member
Ok I am lost. It would seem that I need to reload the Datagrid but so far I have not been able to accomplish that while the form is open. The only way I can get the changes to show is to close the form and reopen.
Here is the code but I can't figure out how to refresh the adapter. Requesting a refill does nothing because, my guess, is that it is not hitting the database the second time. It is using the data from memory. Do I need to create a second dataset?
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'This fills the data adapter the first time.
OleDbDataAdapter1.Fill(DsPayroll1)
End Sub
Private Sub cmdDeleteLine_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdDeleteLine.Click
Dim strDataBaseName As String = "c:\path\dbname.mdb"
Dim cnn As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDataBaseName)
Dim cmd As New OleDb.OleDbCommand
Dim getID As Integer
getID = DataGrid1.Item(DataGrid1.CurrentRowIndex, 21)
MsgBox("Rec # " & getID & " has been deleted.", _
MsgBoxStyle.OKOnly, "Record Deleted")
cnn.Open()
cmd.Connection = cnn
Try
cmd.CommandText = "Delete From TimeGr Where ID = " & getID
cmd.ExecuteNonQuery()
Catch exError As Exception
MessageBox.Show(exError.Message)
End Try
cnn.Close()
' This should refill the data adapter but so
' far it has not worked at all.
OleDbDataAdapter1.Fill(DsPayroll1)
End Sub
-
Nov 17th, 2003, 11:38 AM
#6
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|