是否可以从填充了数据库的列表框中删除项目? [英] Is it possible to remove an item from a listbox populated with a database?

查看:63
本文介绍了是否可以从填充了数据库的列表框中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我问如何从填充了数据库的列表框中删除项目之前



是否可以从填充了数据库的列表框中删除项目?



我的尝试:



如果可能,我的代码不起作用:

  Dim  str  As  字符串 
Dim drv As DataRowView = CType (ListBox1.SelectedItem,DataRowView)
str = CStr (drv .Row.Item( PName))
ListBox1.Items.Remove(ListBox1。 SelectedItem(str))



这就是我填充我的列表框的方式:



 '  Pop使用人员名称 
尝试
connection = OleDbConnection( Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \ Nikko Jaze Fabellon \Documents\ASRASIM.accdb
connection.Open()
ds = DataSet
tables = ds.Tables
dataAdapter = OleDbDataAdapter( SELECT [FirstName],[Lastnel]来自[Personnel],其中[Status] ='Activated',connection)
dataAdapter.Fill(ds, 人员
Dim view1 作为 DataView(表格( 0 ))
使用 personnelList
.DataSource = ds.Tables( 人员
.DisplayMember = FirstName
.ValueMember = LastName
.SelectedIndex = 0
结束 使用

connection.Close()

Catch ex As 异常
MessageBox.Show(ex.Message)
结束 尝试





或if这是不可能的,我需要冰想到另一种方式。

解决方案

在我问如何从填充了数据库的列表框中删除项目之前



是否可以从填充了数据库的列表框中删除项目?





直到 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 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!


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#][^]


这篇关于是否可以从填充了数据库的列表框中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆