[英]how to select specific field using a filter from access in C#
in C# and windows Form,在 C# 和 windows 窗体中,
I have a database like this:我有一个这样的数据库:
and this is how I using a class put it's data into a datagridview
:这就是我使用类将其数据放入
datagridview
:
class DBConnection
{
public static void GetList(Form2 frm2)
{
string DBPath = Application.StartupPath;
OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+DBPath+@"\DataBase\SampleFeeds.accdb");
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary", Connection);
DataTable Dtable = new DataTable();
DataA.Fill(Dtable);
frm2.SelectedFeeddataGridView.DataSource = Dtable;
}
}
and this is my form load:这是我的表单加载:
private void Form2_Load(object sender, EventArgs e)
{
DBConnection.GetList(this);
}
So far everything is ok.到目前为止一切正常。
now I have a question:现在我有一个问题:
for example I have a list box FeedSelectListBox
, I want to when user click on a button GrassLegumeForagebtn
my FeedSelectListBox
fill with only all of Feed Names that are in the category of Grass / Legume Forage.例如,我有一个列表框
FeedSelectListBox
,我想当用户单击按钮GrassLegumeForagebtn
我的FeedSelectListBox
,只填充属于 Grass / Legume Forage 类别的所有 Feed Names。
how should I do that ?我该怎么做?
with help of Damirchi My problem solved在 Damirchi 的帮助下我的问题解决了
//----------------- //--
But now I have another question: I want to when user select a feed from list box all of it's data from data base (like name, number, feed type and ,,,) put in a data grid view.但现在我有另一个问题:当用户从列表框中选择一个提要时,我想将数据库中的所有数据(如名称、编号、提要类型和 ,,,)放入数据网格视图中。
I used this code on my SelectFeedbtn
but it doesn't work :我在
SelectFeedbtn
上使用了此代码,但它不起作用:
private void SelectFeedbtn_Click(object sender, EventArgs e)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
string FeedSelectedID = FeedSelectListBox.SelectedValue.ToString();
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID = 'FeedSelectedID'" , Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);
SelectedFeeddataGridView.DataSource = DTable;
}
And ValueMember
property of FeedSelectListBox
is ID but the error is : FeedSelectListBox
ValueMember
属性是 ID 但错误是:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll. System.Data.dll 中发生类型为“System.Data.OleDb.OleDbException”的未处理异常。
I even use this query but it still doesn't work:我什至使用这个查询,但它仍然不起作用:
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID =" FeedSelectListBox.SelectedValue , Connection);
at first you most set the DisplayMember
and ValueMember
properties of FeedSelectListBox
and use this code on click event.首先,您最多设置
FeedSelectListBox
的DisplayMember
和ValueMember
属性,并在单击事件上使用此代码。 you can put this code on GrassLegumeForagebtn
click event.您可以将此代码放在
GrassLegumeForagebtn
单击事件上。
string DBPath = Application.StartupPath;
OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+DBPath+@"\DataBase\SampleFeeds.accdb");
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where category='Grass / Legume'", Connection);
DataTable Dtable = new DataTable();
DataA.Fill(Dtable);
frm2.FeedSelectListBox .DataSource = Dtable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.