-4

I had populate combobox as below my problem is that I want to use displaymember and valuemember with data binding I searched but all examples are used without data binding

    public void Pobulate_combobox()
    {
        string connstr = @"Data Source=ORCL; User Id=user; password=pwd;";
        string cmdtxt = @"SELECT  column1, 
                                  column2
                           FROM table
                            ORDER BY column1";

        using (OracleConnection conn = new OracleConnection(connstr))
        using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
        {
            conn.Open();

            using (OracleDataReader dr = cmd.ExecuteReader())
            {
                List<string> items = new List<string>();

                while (dr.Read())
                {
                    CBOfficeCode.Items.Add(dr.GetString(0));
                }
            }
        }
    }
sam
  • 2,493
  • 6
  • 38
  • 73

1 Answers1

3

To use the DisplayMember and ValueMember properties you need to set the DataSource property of the combo. The easiest way is to load a DataTable and use that instance as DataSource

using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
{
    conn.Open();
    DataTable dt = new DataTable();
    dt.Load(cmd.ExecuteReader());
    CBOfficeCode.DataSource = dt;
    CBOfficeCode.DisplayMember = "Column1";
    CBOfficeCode.ValueMember = "Column2";
}
Steve
  • 213,761
  • 22
  • 232
  • 286