|
-
Dec 23rd, 2009, 12:31 PM
#1
Thread Starter
Addicted Member
[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.
-
Dec 23rd, 2009, 03:53 PM
#2
Hyperactive Member
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.
-
Dec 23rd, 2009, 04:02 PM
#3
Thread Starter
Addicted Member
Re: Delete BindingSource
What's the index? Can you describe better? Thanks!
-
Dec 23rd, 2009, 04:09 PM
#4
Hyperactive Member
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.
-
Dec 23rd, 2009, 04:14 PM
#5
Thread Starter
Addicted Member
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?
-
Dec 23rd, 2009, 04:20 PM
#6
Hyperactive Member
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.
-
Dec 23rd, 2009, 04:30 PM
#7
Thread Starter
Addicted Member
Re: Delete BindingSource
It gives me Error:
Index -1 is either negative or above rows count.
I just copied your code.
-
Dec 23rd, 2009, 04:36 PM
#8
Hyperactive Member
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.
-
Dec 23rd, 2009, 04:40 PM
#9
Thread Starter
Addicted Member
-
Dec 23rd, 2009, 04:48 PM
#10
Hyperactive Member
Re: Delete BindingSource
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.
-
Dec 24th, 2009, 07:27 AM
#11
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.
-
Dec 24th, 2009, 07:45 AM
#12
Thread Starter
Addicted Member
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?
-
Dec 24th, 2009, 07:55 AM
#13
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.
-
Dec 24th, 2009, 08:03 AM
#14
Thread Starter
Addicted Member
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.
-
Dec 24th, 2009, 08:46 AM
#15
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|