繁体   English   中英

在组合框中填写表格值

[英]Fill the table value in combobox

将VS2005与C#结合使用

我想通过使用表值来填充组合框。

OdbcConnection con = new OdbcConnection();
    OdbcCommand cmd;

con.ConnectionString =                            "";
        con.Open();
        cmd = new OdbcCommand("Select no from table", con);
        ada = new OdbcDataAdapter(cmd);
        ds = new DataSet();
        ada.Fill(ds);
        combobox1.Items.Add(ds);

但是组合框中没有加载任何值,我上面提到的代码有什么问题。

可以为探针提供解决方案吗?

您的实际连接字符串中确实有东西,对吗?

您正在将数据加载到DataSet中 -这是表和关系的集合。 组合框应如何知道要显示的表数据? 如果DataSet有多个表,则必须另外定义要使用的表之一。 如果DataSet有内只有一个表,那么它的资源的浪费使用DataSet位居第一。

如果只有一组数据,请改用DataTable

con.Open();
cmd = new OdbcCommand("Select no from table", con);
ada = new OdbcDataAdapter(cmd);
DataTable data = new DataTable();
ada.Fill(data);

// define the column to be used to display text in the combobox
combobox1.DataTextField = "FirstName";
// define the column to be used as the value for the selection in the combobox
combobox1.DataValueField = "CustomerID";

combobox1.DataSource = data;
combobox1.DataBind();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM