Combox下拉绑定DataGridView

Posted 小A永不败

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Combox下拉绑定DataGridView相关的知识,希望对你有一定的参考价值。

这个Combox下拉很多人都在用  但其中绘制的语句如下:

    #endregion
        #region 方法
        #region 绘制DataGridView以及下拉DataGridView
        private void DrawDataGridView()
        {
            DataGridView dataGridView = new DataGridView();
            dataGridView.ScrollBars = ScrollBars.Both;
            dataGridView.AutoSize = true;
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToDeleteRows = false;
            dataGridView.AllowUserToResizeRows = false;
            dataGridView.Dock = DockStyle.Fill;
            dataGridView.DataSource = null;
            
            dataGridView.BackgroundColor = SystemColors.Control;
            dataGridView.BorderStyle = BorderStyle.None;
            dataGridView.ReadOnly = true;
            dataGridView.AllowUserToAddRows = false;
            dataGridView.RowHeadersVisible = false;
            dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView.DoubleClick += new EventHandler(dataGridView_DoubleClick);
            dataGridView.KeyDown += new KeyEventHandler(dataGridView_KeyDown);

           //设置DataGridView的数据源
            Form frmDataSource = new Form();
            frmDataSource.Controls.Add(dataGridView);
              
            dataGridViewHost = new ToolStripControlHost(dataGridView);
            dataGridViewHost.AutoSize = m_blPopupAutoSize;     
            TextBox textBox = new TextBox();
            textBox.Width = 200;
            textBox.TextChanged += new EventHandler(textBox_TextChanged);
            textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
            textBoxHost = new ToolStripControlHost(textBox);
            textBoxHost.AutoSize = false;
            
            dropDown = new ToolStripDropDown();
            dropDown.Items.Add(textBoxHost);
            dropDown.Items.Add(dataGridViewHost);
            dropDown.AutoSize = true;
        }
        #endregion

这个过程也很好理解  一个ToolStripDropDown控件  里面放了两个ToolStripControlHost,单机Combox就让这个ToolStripDropDown控件显示就行,问题在这句
            //设置DataGridView的数据源
            Form frmDataSource = new Form();
            frmDataSource.Controls.Add(dataGridView);
不理解其中的原因,dataGridView的dataSource是在程序外调用的,但是不用这句,下拉框里不绑定数据,为了让更直观理解 ,在放个例子:

public partial class Form2 : Form
    {
        private ToolStripControlHost dataGridViewHost;
        private ToolStripDropDown dropDown;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            DataGridView dataGridView = new DataGridView();
            //Form frmDataSource = new Form();
            //frmDataSource.Controls.Add(dataGridView);            
            //或者这个窗体的任何一个容器类添加这个dataGridView
            dataGridView.Dock = DockStyle.Fill;
            dataGridView.DataSource = createStudentTable();
           
            //设置DataGridView的数据源
           
            dataGridViewHost = new ToolStripControlHost(dataGridView);   
            dropDown = new ToolStripDropDown();      
 
            dropDown.Items.Add(dataGridViewHost);
            contextMenuStrip1.Items.Add(dataGridViewHost);        
        }

        /// <summary>
        /// 生产测试数据
        /// </summary>
        /// <returns></returns>
        private DataTable createStudentTable()
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("学号");
            dt.Columns.Add(dc);
            dc = new DataColumn("姓名");
            dt.Columns.Add(dc);
            dc = new DataColumn("性别");
            dt.Columns.Add(dc);
            dc = new DataColumn("专业");
            dt.Columns.Add(dc);
            dc = new DataColumn("入学年份");
            dt.Columns.Add(dc);

            DataRow dr = dt.NewRow();
            dr["学号"] = "20010101";
            dr["姓名"] = "刘德华";
            dr["性别"] = "";
            dr["专业"] = "影视表演";
            dr["入学年份"] = "2001";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["学号"] = "20010702";
            dr["姓名"] = "张学友";
            dr["性别"] = "";
            dr["专业"] = "计算机科学技术";
            dr["入学年份"] = "2001";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["学号"] = "20010403";
            dr["姓名"] = "郭富城";
            dr["性别"] = "";
            dr["专业"] = "哲学系";
            dr["入学年份"] = "2001";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["学号"] = "20010204";
            dr["姓名"] = "柳岩";
            dr["性别"] = "";
            dr["专业"] = "模特专业(裸模方向)";
            dr["入学年份"] = "2001";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["学号"] = "20010305";
            dr["姓名"] = "郭德纲";
            dr["性别"] = "";
            dr["专业"] = "中文系";
            dr["入学年份"] = "2001";
            dt.Rows.Add(dr);

            return dt;
        }
    }

问题还是这样  如果不添加  右键弹出的GridView没有数据  注释掉那句  就有数据了,求解答。懂的人说

以上是关于Combox下拉绑定DataGridView的主要内容,如果未能解决你的问题,请参考以下文章

C# 控制datagridview的combox属性的列绑定数据

c#里面,ComBox下拉框比较里面,是不是有这个值?

关于datagridview里使用combox的总结

c#中我想在combox里绑定数据库中某字段的内容,并显示在combox的下拉框中,请问大家该怎么去弄呢?

C# 如何使datagridview中的单元格处于可编辑

C# winform dataGridView 里下拉列表,用写代码的方式绑定数据,班级一列要显示为下拉列表