Problem with datagridview

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

    #1

    Problem with datagridview

    Hi all,

    This is the situation...

    DatagridView Control with datasource set to datatable.

    I want the user to be able to delete x number of rows by selecting a row
    with mouse then clicking a 'delete' button.

    Then user clicks a 'Commit' button to update the database.

    This is my delete row code:

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    If Me.DataGridView 1.SelectedRows. Count = 0 Then Exit Sub
    Dim x As Integer = bs.Position
    udt.Item(x).Del ete()
    End Sub

    This is my commit changes code:

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    uta.Update(udt)
    End Sub

    The problem is that once I have deleted a row, the datatable and
    datagridview become unsynchronized and the selected row in the datagrid
    isn't the one that will be deleted from the dataset. (I hope that makes
    sense)

    I can keep things in sync by calling datatable.accep tchanges() after the row
    deletion, but then the tableadapter.up date() command doesn't write the
    changes to the database.

    Any helpful suggestions will be greatly appreciated. I can post the complete
    (simplified) example if it will help.

    Kind regards,

    Martin.


  • Cor Ligthert [MVP]

    #2
    Re: Problem with datagridview

    Martin,

    Just something how a datatable is working, I think that you than will easily
    handle your problem.

    A datatable hold rows. All the rows have rowstatuses which have statuses.
    This can be deleted, changed new, unchanged (and one other one but for this
    not important).

    If you do an update all rows with a status deleted, changed and new are
    handled, they use than the SQL commands, Delete, Update and Insert. The
    nonchanged rows are not handled. The state of the row when it was retrieved
    using the select is hold as well in the datarow. This is checked to the
    currentstate of the datatabase. If there is a difference between those,
    there is a currencyerror.

    If the rows are handled correct than the dataadapter.upd ate command has an
    inbuild AcceptChanges command. What AcceptChanges does is setting all rows
    to unchanged and removes the change information. By that it becomes again
    equal to the current state of the database.

    Be aware that this is very confusing in your code, if this is true I would
    change it as quick as possible.

    udt.Item(x).Del ete(). I can not find what this means.Be also aware that you
    should do deletes in a collection always bottom up, you will become sure in
    problem if you don't doe that, because otherwise the position is changing.

    I hope that this gives you the information to handle your problem.

    Cor

    "Martin" <martin@martin. comschreef in bericht
    news:B4Tgh.4004 $1W1.1256@newsf e4-win.ntli.net...
    Hi all,
    >
    This is the situation...
    >
    DatagridView Control with datasource set to datatable.
    >
    I want the user to be able to delete x number of rows by selecting a row
    with mouse then clicking a 'delete' button.
    >
    Then user clicks a 'Commit' button to update the database.
    >
    This is my delete row code:
    >
    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    If Me.DataGridView 1.SelectedRows. Count = 0 Then Exit Sub
    Dim x As Integer = bs.Position
    udt.Item(x).Del ete()
    End Sub
    >
    This is my commit changes code:
    >
    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    uta.Update(udt)
    End Sub
    >
    The problem is that once I have deleted a row, the datatable and
    datagridview become unsynchronized and the selected row in the datagrid
    isn't the one that will be deleted from the dataset. (I hope that makes
    sense)
    >
    I can keep things in sync by calling datatable.accep tchanges() after the
    row deletion, but then the tableadapter.up date() command doesn't write the
    changes to the database.
    >
    Any helpful suggestions will be greatly appreciated. I can post the
    complete (simplified) example if it will help.
    >
    Kind regards,
    >
    Martin.
    >

    Comment

    • Martin

      #3
      Re: Problem with datagridview

      Hi Cor,

      Thanks for the response, I agree, my example wasn't very clear, sorry about
      that.

      I have, however been tinkering since my first post and have come up with a
      resonable solution; To the grid I add a non-databound extra column and once
      the grid is populated I fill each cell in this column with the index of the
      containing row. I then use this value to calculate which row to delete when
      I select a row.

      This approach enables me to delete rows in an arbitrary order and then
      either commit or reject the changes as I wish.

      I'm sure it's a design issue that has led to this problem in the first
      place, but as a relative beginner to vb2005 and database programming in
      general I'm still very much on a learning curve.

      Once again, thanks,

      Martin.
      "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
      news:%23gi1iVSI HHA.1248@TK2MSF TNGP02.phx.gbl. ..
      Martin,
      >
      Just something how a datatable is working, I think that you than will
      easily handle your problem.
      >
      A datatable hold rows. All the rows have rowstatuses which have statuses.
      This can be deleted, changed new, unchanged (and one other one but for
      this not important).
      >
      If you do an update all rows with a status deleted, changed and new are
      handled, they use than the SQL commands, Delete, Update and Insert. The
      nonchanged rows are not handled. The state of the row when it was
      retrieved using the select is hold as well in the datarow. This is checked
      to the currentstate of the datatabase. If there is a difference between
      those, there is a currencyerror.
      >
      If the rows are handled correct than the dataadapter.upd ate command has an
      inbuild AcceptChanges command. What AcceptChanges does is setting all rows
      to unchanged and removes the change information. By that it becomes again
      equal to the current state of the database.
      >
      Be aware that this is very confusing in your code, if this is true I would
      change it as quick as possible.
      >
      udt.Item(x).Del ete(). I can not find what this means.Be also aware that
      you should do deletes in a collection always bottom up, you will become
      sure in problem if you don't doe that, because otherwise the position is
      changing.
      >
      I hope that this gives you the information to handle your problem.
      >
      Cor
      >
      "Martin" <martin@martin. comschreef in bericht
      news:B4Tgh.4004 $1W1.1256@newsf e4-win.ntli.net...
      >Hi all,
      >>
      >This is the situation...
      >>
      >DatagridView Control with datasource set to datatable.
      >>
      >I want the user to be able to delete x number of rows by selecting a row
      >with mouse then clicking a 'delete' button.
      >>
      >Then user clicks a 'Commit' button to update the database.
      >>
      >This is my delete row code:
      >>
      >Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
      >System.EventAr gs) Handles Button3.Click
      >If Me.DataGridView 1.SelectedRows. Count = 0 Then Exit Sub
      >Dim x As Integer = bs.Position
      >udt.Item(x).De lete()
      >End Sub
      >>
      >This is my commit changes code:
      >>
      >Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
      >System.EventAr gs) Handles Button2.Click
      >uta.Update(udt )
      >End Sub
      >>
      >The problem is that once I have deleted a row, the datatable and
      >datagridview become unsynchronized and the selected row in the datagrid
      >isn't the one that will be deleted from the dataset. (I hope that makes
      >sense)
      >>
      >I can keep things in sync by calling datatable.accep tchanges() after the
      >row deletion, but then the tableadapter.up date() command doesn't write
      >the changes to the database.
      >>
      >Any helpful suggestions will be greatly appreciated. I can post the
      >complete (simplified) example if it will help.
      >>
      >Kind regards,
      >>
      >Martin.
      >>
      >
      >

      Comment

      Working...