标签:
这个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没有数据 注释掉那句 就有数据了,求解答。懂的人说
标签:
原文地址:http://www.cnblogs.com/XACOOL/p/5607808.html