Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Before i ask "How to remove an item from a listbox populated with a database"

Is it possible to remove an item from a listbox populated with a database?

What I have tried:

if it is possible, my code is not working:
VB
Dim str As String
            Dim drv As DataRowView = CType(ListBox1.SelectedItem, DataRowView)
            str = CStr(drv.Row.Item("PName"))
            ListBox1.Items.Remove(ListBox1.SelectedItem(str))

This is how i populate my listbox:

VB
'Populate the ListBox with personnelName from personnel '
       Try
           connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
           connection.Open()
           ds = New DataSet
           tables = ds.Tables
           dataAdapter = New OleDbDataAdapter("SELECT [FirstName],[LastName] from [Personnel] where [Status] = 'Activated' ", connection)
           dataAdapter.Fill(ds, "Personnel")
           Dim view1 As New DataView(tables(0))
           With personnelList
               .DataSource = ds.Tables("Personnel")
               .DisplayMember = "FirstName"
               .ValueMember = "LastName"
               .SelectedIndex = 0
           End With

           connection.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try


or if it is not possible, i need advice to think of another way.
Posted
Updated 14-Feb-18 7:53am
v2
Comments
phil.o 13-Feb-18 5:59am    
If the combo box is data bound, then no, you cannot remove an item this way. You have to filter the datasource instead so that it does not return the element you do not want to see.
Filtering a datasource is usually made through the WHERE statement.
WinterPrison 14-Feb-18 8:54am    
i updated my codes, can u help me sir?

WinterPrison[^] wrote:
Before i ask "How to remove an item from a listbox populated with a database"

Is it possible to remove an item from a listbox populated with a database?


Untill a ListBox control is not bind with a datasource, you can easily remove item from that control. Your ListBox object is not bind with data source, so... See:
ListBox.ObjectCollection.Remove Method (Object) (System.Windows.Forms)[^]
How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control | Microsoft Docs[^]
In the other way, you have to update datasource instead of removing ListBox item. See:
How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource | Microsoft Docs[^]
Updating and Rebinding Data Source References[^]
How to: Update a Data Source with Data from a Host Control[^]

Good luck!
 
Share this answer
 
You could use a DataTable like this:
using (SqlConnection conn = new SqlConnection(CONN_STR))
using (SqlDataAdapter da = new sqlDataAdapter("roboticSiteNames", conn)) {
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];

// 1. set DisplayMember and ValueMember
lbSiteCode.DisplayMember = dt.Columns[0].ColumnName;
lbSiteCode.ValueMember = dt.Columns[1].ColumnName;
// 2. set DataSource
lbSiteCode.DataSource = dt;
}
This is from an answer here: Datatable to listbox[^]

Then you can delete rows in the DataTable, see How to: Delete Rows in a DataTable[^]

This can also be done in a DataSet, e.g.
NorthwindDataSet1.Customers.Rows(0).Delete()
 
Share this answer
 
v2
Comments
WinterPrison 14-Feb-18 8:55am    
im using access sir, i updated my codes can u please take a look and help me?
Maciej Los 14-Feb-18 13:55pm    
Seems your answer does not correspond with OP's question.
RickZeeland 14-Feb-18 14:08pm    
Well, I was pointing him in the right direction I hope, updated the answer to clarify things !
Maciej Los 14-Feb-18 14:22pm    
I believe you may be right.
As you are using a DataView now, you can use .RowFilter like this:
dv.RowFilter = "FirstName < 'Peter'";
See: DataView.RowFilter Property (System.Data)[^]
Here is a nice overview: DataView RowFilter Syntax [C#][^]
 
Share this answer
 
v2
Comments
Maciej Los 14-Feb-18 13:56pm    
And this one too...
;(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900