无法在c#.net中动态填充组合框

本文关键字:动态 填充 组合 net | 更新日期: 2024-09-24 15:16:45

我正在编写下面的代码来获取组合框中的数据,但它在组合框中没有显示任何数据,所以请帮助。。。。。下面是我填充组合框的代码。。

string qry = "select ctid,city from city order by city";
cmd = new SqlCommand(qry, con);
ds.Clear();
ad = new SqlDataAdapter(qry,con);
ad.Fill(ds);
MessageBox.Show(ds.Tables[0].Rows[1]["city"].ToString());
comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "city";
comboBox1.ValueMember = "ctid";

在ad.fill(ds)后面的消息框中,我可以看到表中的值,但它没有反映在组合框中。。。

无法在c#.net中动态填充组合框

我在应用程序中使用了相同的逻辑,只是在SqlCommand中将CommandType属性指定为"Text",将CommandText属性指定为查询。

 var sqlDs = new DataSet();
 var sqlComm = new SqlCommand();
        sqlComm.Connection = sqlCon;
        sqlComm.CommandType = CommandType.Text;
        sqlComm.CommandText = query;
        sqlDa = new SqlDataAdapter(sqlComm);
        sqlDa.Fill(sqlDs);

此外,当您指定显示成员和值成员时,无需在选择查询中指定列,它仍然有效。只有在性能基础上,才能指定列名。