user6874839
user6874839

Reputation:

ComboBox not showing text after binding

I just bound my combobox to database but it is not showing me values. However, I can see by the length of dropdown box that values are in it but aren't visible.

I just clicked on a value and it did work and took me to the next form. But how can I make those values visible?

  private void StudentLogin_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(constr);
        con.Open();
        cmd = new SqlCommand("select Std_ID  from Student", con);
        SqlDataReader reader;

        reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("Std_ID", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Load(reader);

        metroComboBox1.ValueMember = "Std_ID";
        metroComboBox1.DisplayMember = "Name";
        metroComboBox1.DataSource = dt;

        con.Close();
    }
     private void metroComboBox1_SelectedIndexChanged(object sender, EventArgs    e)
    {
        string ID = metroComboBox1.SelectedValue.ToString();
    }

Upvotes: 0

Views: 849

Answers (2)

Tyron78
Tyron78

Reputation: 4197

I think the problem is in your query:

cmd = new SqlCommand("select Std_ID from Student", con);

does not contain the name. Instead try the following:

cmd = new SqlCommand("select Std_ID, Name from Student", con);

In order to display the ID instead of the name, put Std_ID as displaymamber as well.

Upvotes: 1

ammu
ammu

Reputation: 258

metroComboBox1.DataSource = dt;
metroComboBox1.ValueMember = "Std_ID";
metroComboBox1.DisplayMember = "Name";
metroComboBox1.DataBind();

Upvotes: 2

Related Questions