sublimation
asked on
ListBox VB.Net Forms
Hello, I have a listbox on a VB.Net form which hast been populated using the following method:
Me.ListBox1.DataSource = dsResearchers.Tables("tbl_ Panel")
This works fine, but i cant then remove an item from the listbox because it is bound to the datatable. Should I use a different object to the DataAdapter and Datatable?
How could i populate the listbos like i have done in older vewrsions of vb, like below but in VB.Net:
RecordSet.MoveFirst
Do While RecordSet not EOF
ListBox1.AddItem RecordSet(0)
RecordSet.MoveNext
Loop
Thanks
Me.ListBox1.DataSource = dsResearchers.Tables("tbl_
This works fine, but i cant then remove an item from the listbox because it is bound to the datatable. Should I use a different object to the DataAdapter and Datatable?
How could i populate the listbos like i have done in older vewrsions of vb, like below but in VB.Net:
RecordSet.MoveFirst
Do While RecordSet not EOF
ListBox1.AddItem RecordSet(0)
RecordSet.MoveNext
Loop
Thanks
ASKER
Hello, Esteban.
Thanks for the advice. I wont be removing the values from the underlying table because I would end up in a jobless :-)
Is there a way of looping through a recorset and adding the items to the listbox one by one?
Thanks
Thanks for the advice. I wont be removing the values from the underlying table because I would end up in a jobless :-)
Is there a way of looping through a recorset and adding the items to the listbox one by one?
Thanks
What you could do is create another copy of the table and then remove the data you will not need. Finally bind the new table to the ListBox.
DataTable bkpTable = dsResearchers.Tables("tbl_ Panel");
DataRow rowToBeDeleted = **** // Row that you want to delete;
bkpTable.Rows.Remove(rowTo BeDeleted) ;
Me.ListBox1.DataSource = bkpTable;
Me.ListBox1.DataBind();
DataTable bkpTable = dsResearchers.Tables("tbl_
DataRow rowToBeDeleted = **** // Row that you want to delete;
bkpTable.Rows.Remove(rowTo
Me.ListBox1.DataSource = bkpTable;
Me.ListBox1.DataBind();
ASKER
Hello gillit
I cant do that as I am populating a listbox and then when somebody selects an item it goes into an adjacent listbox and in any direction, a bit like one of those field selector wizards. Writing to the database on every click (which will be frequent) would be too slow. I really need a way of looping through a recordset and using the Item.Add method so I can populate the listbox just once.
Thanks
I cant do that as I am populating a listbox and then when somebody selects an item it goes into an adjacent listbox and in any direction, a bit like one of those field selector wizards. Writing to the database on every click (which will be frequent) would be too slow. I really need a way of looping through a recordset and using the Item.Add method so I can populate the listbox just once.
Thanks
Another option is if you know what needs to be filtered initially you can make your datatable only hold the results of a select statement. Then bind to that datatable.
Tables within a dataset are treated like they are tables when bound, but they don't have to mimic a single table from the database. The select statement can select from multiple tables into one table in the dataset and you will be able to treat all the data like it is in one table.
Have fun.
Tables within a dataset are treated like they are tables when bound, but they don't have to mimic a single table from the database. The select statement can select from multiple tables into one table in the dataset and you will be able to treat all the data like it is in one table.
Have fun.
ASKER
I want to open a database connection, populate a listbox then close database connection. I dont want anything bound or tables' data changing. Simply populate a listbox on the form_load event and then drop the connection. Can I loop through a recordset to do this?
Thanks
Thanks
I didn't say remove from database... i said remove from dataset. Database won't be affected if you don't send changes back
I agree with esteban_felipe. If you change the table, you are not affecting the actual database. You are basically just copying the data from the database to the table once. Once you have that table, you can do whatever with the table without it affecting the database
ASKER
sorry esteban_felipe,
That doesnt work either as all the data goes when i do that.
That doesnt work either as all the data goes when i do that.
Which forms are you using (ie Windows Form or Web Forms)?
If Web Forms make sure when you pull the data from the database, you put it with
If Not IsPostBack Then
So that when you only load from the database each time the page loads.
If Web Forms make sure when you pull the data from the database, you put it with
If Not IsPostBack Then
So that when you only load from the database each time the page loads.
ASKER
I am using window forms.
It seems you have already pulled the data from the database to the dataset. You should be able to close the database connection right after that.
Since you have the dataset, you can change the dataset all you want without it affecting your database. That is the reason that you put it in the dataset. When esteban_felipe said to remove the row from the table, he means removing the row from the table in the dataset not the row in the database.
So then, every time a user selects to move an item from one list to the other... you want to remove from list1 to list2. To remove the item from list1, you will have to delete the row the user has selected, then rebind the dataset to list1. You will have to rebind list1 to change the values within list1. To add the item to list2, you will have to take the item and add it to another table to bind with list2.
Basically, you should only access the db once but will have to rebind the listbox each time you want to change their views.
Since you have the dataset, you can change the dataset all you want without it affecting your database. That is the reason that you put it in the dataset. When esteban_felipe said to remove the row from the table, he means removing the row from the table in the dataset not the row in the database.
So then, every time a user selects to move an item from one list to the other... you want to remove from list1 to list2. To remove the item from list1, you will have to delete the row the user has selected, then rebind the dataset to list1. You will have to rebind list1 to change the values within list1. To add the item to list2, you will have to take the item and add it to another table to bind with list2.
Basically, you should only access the db once but will have to rebind the listbox each time you want to change their views.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nice solution iboutchkine.
sorry sub, I guess I was wrong about the binding.
sorry sub, I guess I was wrong about the binding.
Hi sublimation,
I agree with iboutchkine's solution.
Simply do not bind!
I myself never bind and use similar techniques to the one presented by his answer, most often with ListViews (which I find are much better in the long run to ListBoxes)
Dabas
I agree with iboutchkine's solution.
Simply do not bind!
I myself never bind and use similar techniques to the one presented by his answer, most often with ListViews (which I find are much better in the long run to ListBoxes)
Dabas
ASKER
Nice, iboutchkine!
Thanks!
Thanks!
As is databound, you will have to remove the row from the table and bind it again.
Esteban Felipe
www.estebanf.com