在c#中使用Access数据库中的DataTable填充ComboBox [英] Filling ComboBox using DataTable from Access database in c#
问题描述
我在MS Access数据库中有一个两列表,并希望在ComboBox中填充其第二列,用户可以从中进行选择。我正在使用DataTable来做到这一点。代码如下:
private void Form1_Load( object sender,EventArgs e)
{
OleDbConnection connect = new OleDbConnection( @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C :\ Users\Anas\Dropbox\C#Projects \ 2.Database\ClOsODb.accdb; Persist Security Info = False);
connect.Open();
OleDbCommand cmd = new OleDbCommand( select *来自课程编号和名称,连接);
OleDbDataReader odr = null ;
odr = cmd.ExecuteReader();
DataTable table = new DataTable();
table.Load(odr);
course_Number_and_NameComboBox.DataSource = table;
course_Number_and_NameComboBox.BindingContext = this .BindingContext;
course_Number_and_NameComboBox.DisplayMember = 课程编号和名称;
course_Number_and_NameComboBox.ValueMember = ID;
connect.Close();
}
然而,问题是当我运行应用程序时,ComboBox是空的!它没有被填满。任何人都可以帮助我,告诉我哪里错了,我该怎么办?非常感谢帮助。
此外,有人可以告诉我如何在选择时获取DataTable所选行的第一列中的值在ComboBox中(换句话说,当一个项目将在表格中一行的ComboBox - 第二列中选中时 - 我将如何知道在ComboBox中选择哪一行以及如何在第一列中获取该值同一行)?
首先看一下sql命令从课程编号和名称中选择*桌子的名称是什么?就SQL而言,它是课程和数字和名称只是抛出错误 - 这就是为什么你没有填充你的组合框。
如果你真的必须在表名中使用空格(=坏主意)那么你必须用方括号括起表名,所以查询变为从[课程编号和名称]中选择*
这同样适用于同名的列course_Number_and_NameComboBox.DisplayMember = [课程编号和名称];
至于问题的第二部分,当在ComboBox中选择一行时,course_Number_and_NameComboBox.Selected Text
属性将被设置为可见文本(即[课程编号和名称]
中的值)和course_Number_and_NameComboBox.Selected 值
属性将设置为ValueMember
- 即ID中的值
如果您想要数据表中的任何其他数据,则必须根据ID
更改
OleDbCommand cmd = new OleDbCommand( select * from Course Number and Name,connect);
to
OleDbCommand cmd = new OleDbCommand( 从[课程编号和名称]中选择Col1,ID,连接);
给出表中给出的列名,当表名或列名中有空格时也使用[]
然后,
course_Number_and_NameComboBox.DisplayMember = Col1;
course_Number_and_NameComboBox.ValueMember = ID;
I have a two column table in an MS Access database and want to fill its second column in a ComboBox from where the user can make a selection. I'm using a DataTable to do it. The code is below:
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Anas\Dropbox\C# Projects\2.Database\ClOsODb.accdb; Persist Security Info=False");
connect.Open();
OleDbCommand cmd = new OleDbCommand("select * from Course Number and Name", connect);
OleDbDataReader odr = null;
odr = cmd.ExecuteReader();
DataTable table = new DataTable();
table.Load(odr);
course_Number_and_NameComboBox.DataSource = table;
course_Number_and_NameComboBox.BindingContext = this.BindingContext;
course_Number_and_NameComboBox.DisplayMember = "Course Number and Name";
course_Number_and_NameComboBox.ValueMember = "ID";
connect.Close();
}
However, the problem is that when I run the application, the ComboBox is empty! It doesn't get filled. Can anyone help me and tell me where I am wrong and what should I do? Help will be greatly appreciated.
Also, can anyone tell me how will I get the value in the first column of the selected row of the DataTable when a selection is made at the ComboBox(in other words, when an item will be selected in the ComboBox-second column of a row in the table-how will I know which row is selected in the ComboBox and how will I get the value in the first column of the same row)?
Firstly have a look at that sql commandselect * from Course Number and NameWhat is the name of the table? As far as SQL is concerned then it is "Course" and "Number and Name" just throw an error - which is why you don't get your combobox populated.
If you really must use spaces in your table names ( = bad idea ) then you must surround the table name with square brackets, so the query becomesselect * from [Course Number and Name]
The same applies to the column of the same namecourse_Number_and_NameComboBox.DisplayMember = "[Course Number and Name]";
As to the second part of your question, when a row is selected in the ComboBox thecourse_Number_and_NameComboBox.SelectedText
property will be set to the visible text (i.e. the value that was in[Course Number and Name]
) and thecourse_Number_and_NameComboBox.SelectedValue
property will be set to theValueMember
- i.e. the value that was inID
If you want any other data from the datatable you will have to look it up based on the ID
change
OleDbCommand cmd = new OleDbCommand("select * from Course Number and Name", connect);
to
OleDbCommand cmd = new OleDbCommand("select Col1, ID from [Course Number and Name]", connect);
give the column names as you given in your table and also use [] when you have spaces in your table name or column names
then,
course_Number_and_NameComboBox.DisplayMember = "Col1"; course_Number_and_NameComboBox.ValueMember = "ID";
这篇关于在c#中使用Access数据库中的DataTable填充ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!