1

我正在使用下一个代码。

   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...并且我希望它们显示值。

我究竟做错了什么?

4

1 回答 1

2

我认为在您的组合框中,行看起来像显示的那样;

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

您需要设置组合框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());
}
于 2013-01-08T22:03:21.710 回答