Results 1 to 15 of 15

Thread: [RESOLVED] Delete BindingSource

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Resolved [RESOLVED] Delete BindingSource

    Hello!

    I have one simple question.

    What's the code for deleting one row from Database using BindingSource and DataSet? Just like from BindingNavigator.

    I tried with

    Code:
    BindingSource.RemoveCurrent
    But computer doesn't remove CURRENT date, but first or last date. Help. Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Delete BindingSource

    You can remove it by index like this:

    Code:
    BindingSource.RemoveAt(m_index)
    That way you know you're removing the row that you want.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    What's the index? Can you describe better? Thanks!

  4. #4
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Delete BindingSource

    If your table has 5 rows, then the first row is index 0, the second row is index 1 and so on. What are you using to display your data? You should be able to use a SelectedIndex property to get the index of the selected item.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    I have one listbox with dates which are in one database. And I want to remove row from selected item from ListBox. How to?

  6. #6
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Delete BindingSource

    You should be able to do this:

    Code:
    Dim m_index as Integer
    m_index=Listbox1.SelectedIndex
    BindingSource.RemoveAt(m_index)
    That would remove the selected listbox item from the binding source. You would then need to apply an Update method to save the changes.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    It gives me Error:

    Index -1 is either negative or above rows count.

    I just copied your code.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Delete BindingSource

    That was meant as an example, I doubt you could just copy and paste that into your application. Index = -1 means the index your looking for wasn't found. Can you post your code? Then I or someone else can help you better.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    I just copied your code

  10. #10
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Delete BindingSource

    I just copied your code
    I see, sorry about the paste. You can also get index = -1 if no item was selected in the listbox when you execute that code.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Delete BindingSource

    Calling RemoveCurrent on the BindingSource will delete the currently selected record. Is that what you want? If so then that's what you do. If not then RemoveAt will delete the record at a specific index. Instead of asking us what index, you tell us. You must know what record you want to delete. How do you identify it? That will tell you how to get the index.

    Note that, either way, that's only going to delete the record locally. You still have to use a TableAdapter or DataAdapter to Update the database with the changes from the DataTable. You would normally use the same TableAdapter or DataAdapter that you used to Fill your DataTable in the first place.

    Don't just blindly copy and paste code. Read it, think about what it does and then write your own code that implements the same principle but in a way that's appropriate for your app. If you don't understand what it does then do some reading or ask a question.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    Yes, I tried with

    Code:
    Me.BindingSource.RemoveCurrent
    And everything is OK when I navigate with buttons from BindingNavigator (previous, next, first, last) but if I use this code:

    Code:
            Me.BindingSource.Position = Me.BindingSource.Find("Column from DataBase", ListBox.SelectedItem)
    Then, when I click on some item in ListBox, program navigates good, BUT when I click remove with code:

    Code:
    Me.BindingSource.RemoveCurrent
    Program removes first date. What's the problem?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Delete BindingSource

    RemoveCurrent removes the current item, plain and simple. If the first date is removed then that's the current item. I can't tell you more because the information you've provided is insufficient to determine how your app works and is supposed to work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    No!

    I said you, when I use this code for finding dates:

    Code:
            Me.BindingSource.Position = Me.BindingSource.Find("Name", ListBox.SelectedItem)
    And when I press button with code:

    Code:
    Me.BindingSource.RemoveCurrent
    Program removes first item.

    And when I navigate to item I want with BindingNavigator and then I press button remove, everything is OK, program removes item that's current.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Delete BindingSource

    RESOLVED!

    When you click on remove button, just enable Timer and Timer tick code you write listbox.clear, and then again you load dates in listbox and timer1.enabled = false.

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