vs2005 winfoms DataGridView DataGridViewColumn 数据绑定

来源:互联网 发布:云计算工程师的收入 编辑:程序博客网 时间:2022/05/27 13:45
private void SalesModelRalating(DataGridViewColumn cmb)
        {
            string strSql = "SELECT DISTINCT salesModel FROM Product WHERE proState='" + ProductVO.USE_STATE + "'";

            string DBConnent = System.Configuration.ConfigurationSettings.AppSettings["connStr"];
            SqlConnection DBconn = new SqlConnection(DBConnent);
            try
            {
                if (DBconn.State == ConnectionState.Closed)
                {
                    DBconn.Open();
                }

                SqlDataAdapter da = new SqlDataAdapter(strSql, DBconn);
                DataSet ds = new DataSet();

                da.Fill(ds, "Product");
                DataTable dt = ds.Tables["Product"];

                cmb.DataPropertyName = "salesModel";

                //删除自动生成的 cmbSalesModel(业务型号) 列
                this.dGVOrderProductTable.Columns.Remove("cmbSalesModel");

                //为表创建列表列
                DataGridViewComboBoxColumn Lists = new DataGridViewComboBoxColumn();
                Lists.DisplayIndex = 1;
                Lists.HeaderText = "业务型号";

                //此列绑定至 salesModel 字段
                Lists.DataPropertyName = "salesModel";

                //列表从数据源获得数据
                Lists.DataSource = dt;
                Lists.ValueMember = "salesModel";

                //添加该列
                this.dGVOrderProductTable.Columns.Add(Lists);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据绑定异常:" + ex.Message);
            }
            finally
            {
                DBconn.Close();
            }
        }