deleting multiple rows in a datagridview .net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TU1TSkVE?=

    #1

    deleting multiple rows in a datagridview .net

    How to let user delete multi rows from the BindingSource while the
    SelectionMode Property set to RowHeaderSelect
    I have in my program datagridview bound it to sql table
    Throw Bindingsource

    To fill it:

    MyTableTableAda ptor.fill(MyDat aset.Mytable);


    MyTableBindingS ource.datasourc e = MyDataset;
    MyTableBindingS ource.datamembe r = MyDataset;


    Datagridview.da tasource = MyTableBindingS ource;


    To delete from it:

    MyTableBindingS ource. Removecurrent() ;

    This only removes one item at the time so how I can let the user delete
    multi selected rows

    Thank you in advance


  • Cor Ligthert[MVP]

    #2
    Re: deleting multiple rows in a datagridview .net

    As I see that you use a datatable: first the question, do you want to use
    remove? Remove means that it is removed from the collection, not giving the
    dataadapter the change to remove it from the database?

    Cor


    "MMSJED" <MMSJED@discuss ions.microsoft. comschreef in bericht
    news:3B3ED895-85C7-485B-BBB3-91FF92E9BB77@mi crosoft.com...
    How to let user delete multi rows from the BindingSource while the
    SelectionMode Property set to RowHeaderSelect
    I have in my program datagridview bound it to sql table
    Throw Bindingsource
    >
    To fill it:
    >
    MyTableTableAda ptor.fill(MyDat aset.Mytable);
    >
    >
    MyTableBindingS ource.datasourc e = MyDataset;
    MyTableBindingS ource.datamembe r = MyDataset;
    >
    >
    Datagridview.da tasource = MyTableBindingS ource;
    >
    >
    To delete from it:
    >
    MyTableBindingS ource. Removecurrent() ;
    >
    This only removes one item at the time so how I can let the user delete
    multi selected rows
    >
    Thank you in advance
    >
    >

    Comment

    • =?Utf-8?B?TU1TSkVE?=

      #3
      Re: deleting multiple rows in a datagridview .net

      Actually what I am trying to do is to give the user chance to undo changes by
      calling BindingSource.C ancelEdit()
      And making Save button which is updating changes to the database By calling
      the update Tableadaptor method so I have to call Remove or RemoveAt Or what
      do u think?

      To delete multiple rows in a datagridview I am using
      try
      {
      for (int i = Main2DGrdView.S electedCells.Co unt - 1; i >= 0;
      i--)
      {

      mAIN1MAIN2Relat ionBindingSourc e.RemoveAt

      (Main2DGrdView. SelectedCells[i].RowIndex);
      }
      }

      catch (Exception ex)
      {
      MessageBox.Show ("Deleted failed" + "\n" + ex.Message);
      }
      It raise the exception ArgumentOutOfRa ngeException
      So how to force the program to only delete row and jump to next selected row
      and deleted too
      Thank you in advance
      mmsjed


      "Cor Ligthert[MVP]" wrote:
      As I see that you use a datatable: first the question, do you want to use
      remove? Remove means that it is removed from the collection, not giving the
      dataadapter the change to remove it from the database?
      >
      Cor
      >
      >
      "MMSJED" <MMSJED@discuss ions.microsoft. comschreef in bericht
      news:3B3ED895-85C7-485B-BBB3-91FF92E9BB77@mi crosoft.com...
      How to let user delete multi rows from the BindingSource while the
      SelectionMode Property set to RowHeaderSelect
      I have in my program datagridview bound it to sql table
      Throw Bindingsource

      To fill it:

      MyTableTableAda ptor.fill(MyDat aset.Mytable);


      MyTableBindingS ource.datasourc e = MyDataset;
      MyTableBindingS ource.datamembe r = MyDataset;


      Datagridview.da tasource = MyTableBindingS ource;


      To delete from it:

      MyTableBindingS ource. Removecurrent() ;

      This only removes one item at the time so how I can let the user delete
      multi selected rows

      Thank you in advance
      >

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: deleting multiple rows in a datagridview .net

        You have to use the delete methods which leaves the rows in the datatable
        with a rowstate deleted.

        When the update is done, the rows will be removed by the in the dataadapter
        update build in AcceptChanges method which does that.

        Cor

        "MMSJED" <MMSJED@discuss ions.microsoft. comschreef in bericht
        news:EF46FA2F-0F89-4713-90A3-F6BFB095FEBC@mi crosoft.com...
        Actually what I am trying to do is to give the user chance to undo changes
        by
        calling BindingSource.C ancelEdit()
        And making Save button which is updating changes to the database By
        calling
        the update Tableadaptor method so I have to call Remove or RemoveAt Or
        what
        do u think?
        >
        To delete multiple rows in a datagridview I am using
        try
        {
        for (int i = Main2DGrdView.S electedCells.Co unt - 1; i >= 0;
        i--)
        {
        >
        mAIN1MAIN2Relat ionBindingSourc e.RemoveAt
        >
        (Main2DGrdView. SelectedCells[i].RowIndex);
        }
        }
        >
        catch (Exception ex)
        {
        MessageBox.Show ("Deleted failed" + "\n" + ex.Message);
        }
        It raise the exception ArgumentOutOfRa ngeException
        So how to force the program to only delete row and jump to next selected
        row
        and deleted too
        Thank you in advance
        mmsjed
        >
        >
        "Cor Ligthert[MVP]" wrote:
        >
        >As I see that you use a datatable: first the question, do you want to use
        >remove? Remove means that it is removed from the collection, not giving
        >the
        >dataadapter the change to remove it from the database?
        >>
        >Cor
        >>
        >>
        >"MMSJED" <MMSJED@discuss ions.microsoft. comschreef in bericht
        >news:3B3ED89 5-85C7-485B-BBB3-91FF92E9BB77@mi crosoft.com...
        How to let user delete multi rows from the BindingSource while the
        SelectionMode Property set to RowHeaderSelect
        I have in my program datagridview bound it to sql table
        Throw Bindingsource
        >
        To fill it:
        >
        MyTableTableAda ptor.fill(MyDat aset.Mytable);
        >
        >
        MyTableBindingS ource.datasourc e = MyDataset;
        MyTableBindingS ource.datamembe r = MyDataset;
        >
        >
        Datagridview.da tasource = MyTableBindingS ource;
        >
        >
        To delete from it:
        >
        MyTableBindingS ource. Removecurrent() ;
        >
        This only removes one item at the time so how I can let the user delete
        multi selected rows
        >
        Thank you in advance
        >
        >
        >>

        Comment

        • =?Utf-8?B?TU1TSkVE?=

          #5
          Re: deleting multiple rows in a datagridview .net

          Sorry I did not get you
          Please could you tell me How

          Thank you in advance
          mmsjed

          "Cor Ligthert[MVP]" wrote:
          You have to use the delete methods which leaves the rows in the datatable
          with a rowstate deleted.
          >
          When the update is done, the rows will be removed by the in the dataadapter
          update build in AcceptChanges method which does that.
          >
          Cor
          >
          "MMSJED" <MMSJED@discuss ions.microsoft. comschreef in bericht
          news:EF46FA2F-0F89-4713-90A3-F6BFB095FEBC@mi crosoft.com...
          Actually what I am trying to do is to give the user chance to undo changes
          by
          calling BindingSource.C ancelEdit()
          And making Save button which is updating changes to the database By
          calling
          the update Tableadaptor method so I have to call Remove or RemoveAt Or
          what
          do u think?

          To delete multiple rows in a datagridview I am using
          try
          {
          for (int i = Main2DGrdView.S electedCells.Co unt - 1; i >= 0;
          i--)
          {

          mAIN1MAIN2Relat ionBindingSourc e.RemoveAt

          (Main2DGrdView. SelectedCells[i].RowIndex);
          }
          }

          catch (Exception ex)
          {
          MessageBox.Show ("Deleted failed" + "\n" + ex.Message);
          }
          It raise the exception ArgumentOutOfRa ngeException
          So how to force the program to only delete row and jump to next selected
          row
          and deleted too
          Thank you in advance
          mmsjed


          "Cor Ligthert[MVP]" wrote:
          As I see that you use a datatable: first the question, do you want to use
          remove? Remove means that it is removed from the collection, not giving
          the
          dataadapter the change to remove it from the database?
          >
          Cor
          >
          >
          "MMSJED" <MMSJED@discuss ions.microsoft. comschreef in bericht
          news:3B3ED895-85C7-485B-BBB3-91FF92E9BB77@mi crosoft.com...
          How to let user delete multi rows from the BindingSource while the
          SelectionMode Property set to RowHeaderSelect
          I have in my program datagridview bound it to sql table
          Throw Bindingsource

          To fill it:

          MyTableTableAda ptor.fill(MyDat aset.Mytable);


          MyTableBindingS ource.datasourc e = MyDataset;
          MyTableBindingS ource.datamembe r = MyDataset;


          Datagridview.da tasource = MyTableBindingS ource;


          To delete from it:

          MyTableBindingS ource. Removecurrent() ;

          This only removes one item at the time so how I can let the user delete
          multi selected rows

          Thank you in advance


          >
          >
          >

          Comment

          • Steve Gerrard

            #6
            Re: deleting multiple rows in a datagridview .net

            MMSJED wrote:
            Sorry I did not get you
            Please could you tell me How
            >
            Using RemoveAt on a BindingSource will carry the deletion through to the
            underlying dataset, so there is nothing wrong with using that.

            I'm not sure if CancelEdit will cancel only the last RemoveAt, or all removals.
            If not, you can always do RejectChanges on the entire dataset to get an undo.

            Rather than SelectedCells, you should be doing SelectedRows (still going from
            Count - 1 to 0). That's why your index is going out of range.


            Comment

            • =?Utf-8?B?TU1TSkVE?=

              #7
              Re: deleting multiple rows in a datagridview .net

              How to fix the index is going out of range this is my main question
              I believe this answer need expert


              "Steve Gerrard" wrote:
              MMSJED wrote:
              Sorry I did not get you
              Please could you tell me How
              >
              Using RemoveAt on a BindingSource will carry the deletion through to the
              underlying dataset, so there is nothing wrong with using that.
              >
              I'm not sure if CancelEdit will cancel only the last RemoveAt, or all removals.
              If not, you can always do RejectChanges on the entire dataset to get an undo.
              >
              Rather than SelectedCells, you should be doing SelectedRows (still going from
              Count - 1 to 0). That's why your index is going out of range.
              >
              >
              >

              Comment

              Working...