Help with editing a selecting row in datagrid

OMRebel

Active member
Joined
Sep 27, 2006
Messages
44
Programming Experience
Beginner
Hey everyone. I'm trying to get this form setup to where a user can edit a selected row in a datagrid. I have it setup where the user can double click on the row, and it populates the information for them to edit, but it is adding a new row. I'm not sure how to get it to just edit the selected row. Right now, I'm just updating the data adapter, as I'm not wanting to mess up the database.

Here is the code for the btnUpdate:
VB.NET:
  con3.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = profees.mdb"
        con3.Open()
        sql3 = "Select * From tblTrans where ID = " & txtTransID.Text
        da3 = New OleDb.OleDbDataAdapter(sql3, con3)
        da3.Fill(ds3, "Changes")
        con3.Close()

        MaxRows = ds3.Tables("Changes").Rows.Count   ' Stores how many rows are in the DataSet
        inc = MaxRows
 
        Dim cb As New OleDb.OleDbCommandBuilder(da3) ' Command Builder
        Dim dsNewRow As DataRow ' DataRow Object - needed for adding new rows to DataSet
        dsNewRow = ds3.Tables("Changes").NewRow()  ' Creates the new DataRow object, stores it in dsNewRow variable

        ' Update the Dataset
        dsNewRow.Item("ID") = txtTransID.Text
        dsNewRow.Item("Post") = dtpPost.Value
        dsNewRow.Item("DOS") = dtpDOS.Value
        dsNewRow.Item("Account") = txtPat.Text
        dsNewRow.Item("Charge") = txtCharge.Text
        dsNewRow.Item("Insurance") = cboInsurance.Text
        dsNewRow.Item("Physician") = cboPhysician.Text

        ds3.Tables("Changes").Rows.Add(dsNewRow)  ' This method adds the Row to the DataSet
        da3.Update(ds3, "Changes")  ' DataAdapter update

    End Sub

Any help would be much appreciated. Thanks!
 
The code itself is well explained. You want to update the edited data back to the dataset and database, but your are creating a new row and append to the dataset.

Why don't try to retrieve a row from the dataset with specific key? For example:

dsEditRow = ds3.Tables("Changes").Select("Key = 1")
 
I'm not familiar with how that works to be honest. I'm still very much in a learning phase. I did a google search to see what I could find to explain how the "key" works, but really didn't come up with anything.

Can you break down the code for me on that a bit more? I'm going to continue searching online to see if I can find something that would explain that process a bit more in the meantime.

Thanks.
 
Sorry for lack of explaination. The "Key" I refer here I think should be equal to your ID field in the database. It should be unique and allow you to retrieve the datarow. If your ID is unique, then you can retrieve the datarow:

dsEditRows = ds3.Tables("Changes").Select("ID = " & txtTransID.Text)
 
What would I declare the dsEditRows as? If I declare it as DataRow, then on:
dsEditRows = ds3.Tables("Changes").Select("ID = " & txtTransID.Text)
I get:
Value of type '1-dimensional arry of System.Data.DataRow' cannot be converted to 'System.Data.DataRow'.
 
declare it of type '1-dimensional array of System.Data.DataRow'

VB.NET:
dim dsEditRows() as System.Data.DataRow
 
Back
Top