欢迎DaLao指导

博客名称

你的一句话介绍

在Datagridview中添加datagridviewComboBox列并显示下拉列表

在DataGridView中自动的添加Column。

        private void button_autoAddColumn_Click(object sender, EventArgs e)
        {
            try
            {
                /*链接数据库并获取数据*/
                string connString = "Server=.;Database=testDatabase;Integrated Security=SSPI";
                SqlConnection conn = new SqlConnection(connString);
                string queryString = "select forename,surname,ISNULL(reportsto,0) as reprotsTo from imployee";
                SqlDataAdapter da = new SqlDataAdapter(queryString, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "imployee");
                queryString = "select forename+' '+surname as name ,reportsto from imployee union select '0','(None)'";
                da = new SqlDataAdapter(queryString, conn);
                da.Fill(ds, "manager");



            /**参照C#高级编程书的Datagridview使用combobox列的代码编写**/
            
            SetupColumn(ds);//调用添加Datagridview列的函数
            this.dataGridView2.RowTemplate.Height = 20;

            this.dataGridView2.AutoGenerateColumns = false;//要想动态添加Column,就必须设置为true
            this.dataGridView2.DataSource = ds.Tables["imployee"];
            }
            catch (SqlException exp)
            {
                MessageBox.Show("数据库链接读取数据过程中出现错误:" + exp.Message);
            }
            
        }

        private void SetupColumn(DataSet ds)
        {
            DataGridViewTextBoxColumn deptcode = new DataGridViewTextBoxColumn();
            deptcode.DataPropertyName = "deptcode";
            deptcode.HeaderText = "deptcode";
            deptcode.ValueType = typeof(string);
            deptcode.Frozen = true;
            this.dataGridView2.Columns.Add(deptcode);


            DataGridViewTextBoxColumn deptname = new DataGridViewTextBoxColumn();
            deptname.DataPropertyName = "deptname";
            deptname.HeaderText = "deptname";
            deptname.ValueType = typeof(string);
            deptname.Frozen = true;
            this.dataGridView2.Columns.Add(deptname);

            DataGridViewImageColumn dgvimageColumn = new DataGridViewImageColumn();
            dgvimageColumn.DataPropertyName = "dgvimageColumn";
            dgvimageColumn.HeaderText = "dgvimageColumn";
            dgvimageColumn.ValueType = typeof(Image);
            dgvimageColumn.Frozen = true;
            this.dataGridView2.Columns.Add(dgvimageColumn);

            


            DataGridViewComboBoxColumn dgvComboboxColumn = new DataGridViewComboBoxColumn();
            dgvComboboxColumn.DataPropertyName = "dgvComboboxColumn";
            dgvComboboxColumn.DataSource = this.getTable().DefaultView;
            dgvComboboxColumn.DisplayMember = "forename";
            dgvComboboxColumn.ValueMember = "forename";
            dgvComboboxColumn.HeaderText = "dgvComboboxColumn";
            dgvComboboxColumn.ValueType = typeof(Image);
            
            dgvComboboxColumn.Frozen = true;

                this.dataGridView2.Columns.Add(dgvComboboxColumn);

            DataGridViewLinkColumn dgvLinkColumn = new DataGridViewLinkColumn();
            dgvLinkColumn.DataPropertyName = "dgvLinkColumn";
            dgvLinkColumn.HeaderText = "dgvLinkColumn";
            dgvLinkColumn.ValueType = typeof(Image);
            dgvLinkColumn.Frozen = true;
            this.dataGridView2.Columns.Add(dgvLinkColumn);

            DataGridViewCheckBoxColumn dgvCheckBoxColumn = new DataGridViewCheckBoxColumn();
            dgvCheckBoxColumn.DataPropertyName = "CheckBox";
            dgvCheckBoxColumn.HeaderText = "CheckBox Column";
            dgvCheckBoxColumn.ValueType = typeof(bool);
            dgvCheckBoxColumn.Frozen = true;
            this.dataGridView2.Columns.Add(dgvCheckBoxColumn);
            




        }
        private DataTable getTable()
        {
            string connString = "Server=.;Database=testDatabase;Integrated Security=SSPI";
            //string connString = "Server=.;Database=hr;Integrated Security=SSPI"; 
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string queryString = "select distinct forename from imployee";
            DataTable dt = new DataTable();
            SqlDataAdapter sqlda = new SqlDataAdapter(queryString, conn);
            sqlda.Fill(dt);
            return dt;

        }

 

posted @ 2016-06-03 10:52  麻辣咸鱼  阅读(4787)  评论(0编辑  收藏  举报