ComboBox没有正确填充

时间:2009-11-18 09:46:36

标签: c# .net

我使用以下代码填充组合框。它显示:System.Data.DataRowView而不是其中的实际列值。我缺少什么?

        string Query = "SELECT institutename FROM institutemaster";
        DataSet ds = new DataSet();
        MySqlDataAdapter da = new MySqlDataAdapter(Query, ConnectionClass.CN);
        da.Fill(ds, "Institutes");
        cmbInstitutes.DataSource = ds.Tables["Institutes"];

2 个答案:

答案 0 :(得分:2)

您可能需要设置DisplayMemberValueMember属性:

cmbInstitutes.DisplayMember = "NameOfTheColumnForText";
cmbInstitutes.ValueMember = "NameOfTheColumnForValues";

DataValueFieldDataTextField如果它是ASP.NET应用程序。

答案 1 :(得分:1)

您可以使用以下代码在同一个组合框中填充两列。

string Query = "SELECT (institutename +', '+city) as Institute  FROM institutemaster";
 DataSet ds = new DataSet();   
 MySqlDataAdapter da = new MySqlDataAdapter(Query, ConnectionClass.CN);        
da.Fill(ds, "Institutes");    
cmbInstitutes.DataSource = ds.Tables["Institutes"];
cmbInstitutes.DisplayMember = "Institute";
cmbInstitutes.ValueMember = "Institute";