使用访问数据库在c#中填充combobox
问题描述:
SELECT InvoiceDetails.ItemId, Item_master.item_code
FROM (InvoiceDetails INNER JOIN [Transaction] ON InvoiceDetails.Trans_id = Transaction.Trans_id) INNER JOIN Item_master ON InvoiceDetails.ItemId = Item_master.Item_id where (((Transaction.invoice_no)='" + txt_invoice.Text + "'))
我使用此查询获取特殊数据。
我想在 ComboBox
中显示 ItemCode
。
答
参考 - 从数据库中填充Combobox [ ^ ]。
它有一个代码片段,解释如何绑定ComboBox
。
所以,
- 创建
连接
- 通过查询获取数据
- 填写
数据集
- 分配适当的
DataSource
,DisplayMember
和ValueMember
ComboBox
Refer - Fill Combobox from database[^].
It has a code snippet explaining how to Bind theComboBox
.
So,
- Create a
Connection
- Get the data by query
- Fill to a
DataSet
- Assign appropriate
DataSource
,DisplayMember
andValueMember
for theComboBox
使用ADO.Net断开连接模式。
1)创建数据库连接对象
2)创建dataadapter对象和数据集对象。
3)创建sql命令对象并执行dataadapter的数据集对象的Fill方法。
4)现在你有数据集表中的数据。
5)将组合框的数据源属性绑定到该表。
Use ADO.Net disconnected mode for this.
1) create databse connection object
2) create dataadapter object and dataset object.
3) create sql command object and perform the dataadapter's Fill method for dataset object.
4) Now you have data in dataset's table.
5) bind the combo box's data source property to this table.
this.comboBox1.DataSource = dataSource;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Value";
// make it readonly
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
如果您的查询满意,请标记为答案。
问候,
Dheeraj Gupta
Please Mark as Answer if suffice your query.
Regards,
Dheeraj Gupta