更新时间:2023-02-04 10:40:29
在我问如何从填充了数据库的列表框中删除项目之前
是否可以从填充了数据库的列表框中删除项目?
直到ListBox
控件未与数据源绑定,您可以轻松地从该控件中删除项目。您的ListBox
对象未与数据源绑定,因此...请参阅:
ListBox.ObjectCollection.Remove方法(对象)(System.Windows.Forms) [ ^ ]
如何:在Windows窗体ComboBox,ListBox或CheckedListBox控件中添加和删除项目Microsoft Docs [ ^ ]
另一方面,您必须更新数据源而不是删除ListBox
项目。请参阅:
如何:使用BindingSource反映Windows窗体控件中的数据源更新Microsoft Docs [ ^ ]
更新和重新绑定数据源参考 [ ^ ]
如何:更新包含来自主机控件的数据的数据源 [ ^ ]
祝你好运!
您可以使用 DataTable 之类的这个:使用(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.设置DisplayMember和ValueMember
lbSiteCode.DisplayMember = dt.Columns [0] .ColumnName;
lbSiteCode.ValueMember = dt.Columns [1] .ColumnName;
// 2.设置DataSource
lbSiteCode.DataSource = dt;
}这是答案: Datatable to listbox [ ^ ]
然后你可以删除DataTable中的行,参见如何:删除DataTable中的行 [ ^ ]
这也可以在 DataSet 中完成,例如NorthwindDataSet1.Customers.Rows(0).Delete()
当你正在使用 DataView 时,你可以使用.RowFilter
像这样:dv.RowFilter =FirstName<'Peter';请参阅: DataView.RowFilter Property(Sys tem.Data) [ ^ ]
这是一个很好的概述: DataView RowFilter语法[C#] [ ^ 一>
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:
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:
'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.
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 aListBox
control is not bind with a datasource, you can easily remove item from that control. YourListBox
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 removingListBox
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!
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()
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#][^]