Results 1 to 5 of 5

Thread: How to make datagrid editable?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    How to make datagrid editable?

    I searched the archives, but didn't get a clear answer. I can fill a datagrid just fine with the code below, but it doesn't save any changes back to the original table. Changes can be entered, they just don't get written back.
    The examples in the books I have use a dataview, which I'd rather not use.
    What am I missing? Thanks.
    Code:
    Private Sub FillDs()
            Dim cn As New OleDbConnection()
            Dim cmd As New OleDbCommand()
            Dim da As New OleDbDataAdapter()
            Dim strSQL, strPath, strCn, strProj As String
            strSQL = "SELECT * FROM [Mod List]"
            strPath = strRelPath & gstrFilePath
            strCn = strProv & strPath
    
            Try
                cn.ConnectionString = strCn
                cmd = cn.CreateCommand
                cmd.CommandText = strSQL
                da.SelectCommand = cmd
    
                ds.Clear()
                da.Fill(ds, "[Mod List]")
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error Reading " & strProj, MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            Finally
                If cn.State = ConnectionState.Open Then
                    cn.Close()
                End If
            End Try
        End Sub
    
        Private Sub FillDataGrid()
            dgMod.DataSource = ds
            dgMod.DataMember = "[Mod List]"
        End Sub

  2. #2
    Lively Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    71
    You will have to use the rowstate property and the SQLcommandBuilder (for sql databases) object. the rowstate property will determine whether it's an add, update or delete.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Well, one problem is that there are about 60 db's the user can select to connect to. I had an oledbcommandbuilder, but it didn't work. Haven't looked at the rowstate property.

  4. #4
    Lively Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    71
    This is what I use write the changes made in a grid back to the database. For your situation, you may not need to use the rowstate property. I needed it because I was writing the changes made in the grid back to another database and I needed to know if it was an Add, update or delete. "objDA.Update(objDS)" is what actually writes the changes to the database.

    Dim objCB As SqlCommandBuilder
    Dim intRowState As Integer
    Dim dt As New DataTable()
    Dim dRow As DataRow

    'INITIALIZE THE SQLCommandBuilder BY PASSING IN ADAPTER
    objCB = New SqlCommandBuilder(objDA)
    dt = objDS.Tables(0).GetChanges

    If IsNothing(dt) = False Then
    For Each dRow In dt.Rows
    intRowState = dRow.RowState()
    Select Case intRowState
    Case 4 'record added

    Case 8 'record deleted

    Case 16 ' record modified
    End Select
    Next
    End If
    'WRITE THE UPDATE BACK TO THE DATABASE
    objDA.Update(objDS)
    'CHANGES WILL NOLONGER APPEAR AS CHANGES
    objDS.AcceptChanges()
    grdCurrent().DataSource = objDS

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Thanks, that comes close. I put this in a button click event, and added a messagebox to the select case just for testing, and it correctly selected modified.
    But it threw an exception on the update command:
    Update unable to find TableMapping['Table'] or DataTable 'Table'.
    The dataset has one table, Mod List.
    I'm using VS 2002, if that makes a difference.

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