Пример #1
0
        protected void LoadCustomers()
        {
            //using (SqlConnection conn = new SqlConnection(ConnString))
            //{
            //SqlCommand cmd = new SqlCommand("SELECT companyname,customerid FROM customers", conn);
            //cmd.Connection.Open();
            //SqlDataReader reader = cmd.ExecuteReader();
            //CustomerList.DataSource = reader;
            //CustomerList.DisplayMember = "CompanyName";    //Does not work!
            //CustomerList.ValueMember = "CustomerId";       //Does not work!
            // cmd.Connection.Close();
            //}

            // CustomerList.DataSource = reader;
            // the DataReader is connected only, read only & forward only data.
            // Well, if you were wondering if it could be bound to in WindowsForms,
            // you thought right.  It definitely CAN NOT be bound to, because again,
            // in WindowsForms, you're binding to the actual objects and if you close
            // a DataReader, then all the data is no longer there.  The other big reason
            // you wouldn't be able to bind to a DataReader is that only instances that
            // Implement IList can be bound to in WindowsForms.

            // Add a binding source to be able to use the northwind and poulate the DataSet


            BindingSource bsCustomers;

            bsCustomers = new BindingSource();

            CustomersManager objCustomersManager = new CustomersManager();

            bsCustomers.DataSource = objCustomersManager.GetListofCustomers();


            CustomerList.DataSource    = bsCustomers;
            CustomerList.DisplayMember = "CompanyName";
            CustomerList.ValueMember   = "CustomerId";
        }