How to use a datagridview to add/update/edit rows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JeremyGrand

    How to use a datagridview to add/update/edit rows

    I've read what's available here, but can't seem to make this work right. I'm
    experimenting with components on a form, although I'd rather create the
    pieces & assemble them in code, but that's another issue entirely.

    I have a table in sql2k with an autoincrement column as primary index. It
    also has a changeDate column with a trigger that updates to current datetime
    when any column other than itself is changed.

    Made a new form, followed Cor's tip & used Data to add a datasource.
    Attached the bindingsource to the grid. To save the data, I'm doing this:

    Private Sub DataGridView1_R owLeave(ByVal sender As Object, ByVal e As _
    System.Windows. Forms.DataGridV iewCellEventArg s) Handles
    DataGridView1.R owLeave
    Try
    Me.Validate()
    Me.MoveTicketCr ossReferenceBin dingSource.EndE dit()
    Me.MoveTicketCr ossReferenceTab leAdapter.Updat e(Me.MyDataset. MoveTicketCross Reference)
    Catch ex As Exception
    End Try
    end sub

    I can now view & edit the data, most of the time. But,
    - the trigger usually does not fire, although sometimes it updates every row
    with an identical date.
    - The grid does not refresh -- gotta exit & open it.
    - The autoincrement field increments up every time you enter the (*) row &
    leave it, resulting in gaps in the primary index.
    - Me.MoveTicketCr ossReferenceBin dingSource.Curr ent is not recognized as a
    property. I'd like to look at it to see whether certain things need to be
    done.


  • brennca@gmail.com

    #2
    Re: How to use a datagridview to add/update/edit rows

    First off, I was struggling with entering a new row, and then updating
    the database. Your code helped me fix that (RowLeave event). Thanks.
    Regarding your issue, it sounds like you don't have a where clause in
    your trigger that limits the date update to the current row. If all
    rows are being updated to the current date, you need to limit the
    update to the current row. For grid refresh, you'll need to trigger
    this somehow in your form, I think. A refresh button perhaps. Or some
    other form or control event that would trigger the refresh. Regarding
    the autoincrement, I have a data grid view that is bound to a table
    with an identity (autoincrement) column in a SQLExpress 2005 database.
    I can click in the new row as much as I want, and event edit the
    contents, but if I escape out, thereby canceling the insert, my
    identity field does not increment. So that one I don't know.

    JeremyGrand wrote:
    I've read what's available here, but can't seem to make this work right. I'm
    experimenting with components on a form, although I'd rather create the
    pieces & assemble them in code, but that's another issue entirely.
    >
    I have a table in sql2k with an autoincrement column as primary index. It
    also has a changeDate column with a trigger that updates to current datetime
    when any column other than itself is changed.
    >
    Made a new form, followed Cor's tip & used Data to add a datasource.
    Attached the bindingsource to the grid. To save the data, I'm doing this:
    >
    Private Sub DataGridView1_R owLeave(ByVal sender As Object, ByVal e As _
    System.Windows. Forms.DataGridV iewCellEventArg s) Handles
    DataGridView1.R owLeave
    Try
    Me.Validate()
    Me.MoveTicketCr ossReferenceBin dingSource.EndE dit()
    Me.MoveTicketCr ossReferenceTab leAdapter.Updat e(Me.MyDataset. MoveTicketCross Reference)
    Catch ex As Exception
    End Try
    end sub
    >
    I can now view & edit the data, most of the time. But,
    - the trigger usually does not fire, although sometimes it updates every row
    with an identical date.
    - The grid does not refresh -- gotta exit & open it.
    - The autoincrement field increments up every time you enter the (*) row &
    leave it, resulting in gaps in the primary index.
    - Me.MoveTicketCr ossReferenceBin dingSource.Curr ent is not recognized as a
    property. I'd like to look at it to see whether certain things need to be
    done.

    Comment

    Working...