如何使用select C#填充组合框

时间:2013-01-08 21:58:16

标签: c#

我正在使用下一个代码。

   public void MostrarCombobox(ComboBox cmbIDart)
   {
       Command = "SELECT idArtigoAvaliar FROM dbo.PorAvaliasao WHERE (avaliado = 0) AND (idAutor ='" + Autor.id2 + "')";

       SqlCommand Comm1 = new SqlCommand(Command, Conn);
       SqlDataAdapter data = new SqlDataAdapter(Comm1);

       SqlCommand sqlCommand = new SqlCommand();

       using (Conn)
       {
           sqlCommand = Conn.CreateCommand();

           sqlCommand.CommandText = Command;

           SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, Conn);

           SqlCommandBuilder scb = new SqlCommandBuilder(sda);

           //Criar uma tabela para receber os dados
           DataTable dTable = new DataTable();

           //Preencher a tabela
           sda.Fill(dTable);

           BindingSource bSource = new BindingSource();
           bSource.DataSource = dTable;
           cmbIDart.DataSource = bSource;

           Conn.Close();
       }

   }

我的问题是,当我开始调用ComboBox时,它显示System.Data...,我希望它们显示值。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我认为在你的组合框行中看起来像是显示;

System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView

您需要设置Combobox DisplayMemberValueMember属性。

如果这不能解决您的问题,请尝试动态更清晰。

Combobox1.Items.Clear();

string Command = "SELECT idArtigoAvaliar FROM dbo.PorAvaliasao WHERE (avaliado = 0) AND (idAutor ='" + Autor.id2 + "')";

SqlCommand cmd = new SqlCommand(Command, Conn);
cmd.CommandText = Command;

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
   Combobox1.Items.Add(dr["idArtigoAvaliar"].ToString());
}