简体   繁体   中英

Data binding to ComboBox in c#.net

I'm trying to make a windows application for Library Management. I'm trying to bind the data generated by SQLQuery to comboBoxBranch , but its only showing --Select-- in the options. Is there anything wrong with following code?

private void Form1_Load(object sender, EventArgs e)
{
    bindBranch();
}

private void bindBranch()
{
    OleDbCommand SQLQuery = new OleDbCommand();
    string sqlQueryString = "Select br_id from branch";
    DataTable data = null;

    SQLQuery.Connection = null;
    OleDbDataAdapter dataAdapter = null;
    try
    {
        SQLQuery.CommandText = sqlQueryString;
        SQLQuery.Connection = database;
        data = new DataTable();
        dataAdapter = new OleDbDataAdapter(SQLQuery);
        dataAdapter.Fill(data);
        comboBoxBranch.DisplayMember = "br_id";                
        DataRow dr = data.NewRow();
        dr[0] = "--Select--";
        data.Rows.InsertAt(dr, 0);
        comboBoxBranch.DataSource = data;
    }
    catch (Exception ex)
    {

    }
}

Is there anything else I need to add to some other pages to make this work?

You can bind data like this.

comboBoxBranch.DataSource = new BindingSource { DataSource = data };
comboBoxBranch.DisplayMember = "br_id";
comboBoxBranch.ValueMember = //> something else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM