Results 1 to 6 of 6

Thread: Mapping column in currow of datagrid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    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:
    1. Dim curRow As Integer
    2. curRow = DataGrid1.CurrentRowIndex
    3. MsgBox(curRow)
    4.  
    5. Dim strDataBaseName As String = "C:\path\dbname.mdb"
    6. Dim cnn As New OleDb.OleDbConnection( _
    7.     "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    8.         "Data Source=" & strDataBaseName)
    9. Dim cmd As New OleDb.OleDbCommand
    10. cnn.Open()
    11. cmd.Connection = cnn
    12. Try
    13.     cmd.CommandText = "Delete From TimeGr Where ID = " & 33 ' map this
    14.         ' to the ID in the current row instead of having it hardwired in
    15.         ' the code.
    16.         cmd.ExecuteNonQuery()
    17. Catch exError As Exception
    18.         MessageBox.Show(exError.Message)
    19. End Try

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Imagine the ID column in your grid is the first column for example, then:
    VB Code:
    1. Dim ID as Integer
    2. 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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Post

    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.

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2.         ByVal e As System.EventArgs) Handles Button1.Click
    3.  
    4.         'This fills the data adapter the first time.
    5.         OleDbDataAdapter1.Fill(DsPayroll1)
    6.  
    7.     End Sub
    8.  
    9.     Private Sub cmdDeleteLine_Click(ByVal sender As System.Object, _
    10.         ByVal e As System.EventArgs) Handles cmdDeleteLine.Click
    11.  
    12.         Dim strDataBaseName As String = "c:\path\dbname.mdb"
    13.         Dim cnn As New OleDb.OleDbConnection( _
    14.             "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    15.             "Data Source=" & strDataBaseName)
    16.         Dim cmd As New OleDb.OleDbCommand
    17.         Dim getID As Integer
    18.  
    19.         getID = DataGrid1.Item(DataGrid1.CurrentRowIndex, 21)
    20.         MsgBox("Rec # " & getID & " has been deleted.", _
    21.             MsgBoxStyle.OKOnly, "Record Deleted")
    22.  
    23.         cnn.Open()
    24.         cmd.Connection = cnn
    25.         Try
    26.             cmd.CommandText = "Delete From TimeGr Where ID = " & getID
    27.             cmd.ExecuteNonQuery()
    28.         Catch exError As Exception
    29.             MessageBox.Show(exError.Message)
    30.         End Try
    31.         cnn.Close()
    32.  
    33.         ' This should refill the data adapter but so
    34.         ' far it has not worked at all.
    35.         OleDbDataAdapter1.Fill(DsPayroll1)
    36.  
    37.     End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    ^^ Bump ^^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width