jquery给下拉框赋值-Winform中dataGridView的某个单元格添加下拉框(comboB)

最近的一个需求是实现dataGridView的单元格中下拉框的选择,即加载comboBox控件。 整体思路很简单,通过代码初始化comboBox。 当点击某个单元格时,会触发该单元格的风暴,然后显示下拉框。 当数据被选中时,comboBox被隐藏jquery给下拉框赋值,选中的数据被绑定到单元格的相应位置。

首先构建一个Winform程序并将其推入dataGridView控件中。 构建窗口的Loadstorm,编译一个dataGridView数据初始化方法和一个comboBox初始化方法,并将这两个方法加载到窗口Loadstorm中。

jquery给下拉框赋值_jquery下拉分页_刷百度框下拉

		private ComboBox comboBox = null;
		public Form1()
		{
			InitializeComponent();
		}
		private void Form1_Load(object sender, EventArgs e)
		{
			InitComboBox();
			ShowData();
			
		}
        private void InitComboBox()
		{
			comboBox = new ComboBox();
			this.comboBox.Items.Add("通过");
			this.comboBox.Items.Add("未通过");
			this.comboBox.Leave += new EventHandler(ComboBox_Leave);
			this.comboBox.SelectedIndexChanged += new EventHandler(ComboBox_TextChanged);
			this.comboBox.Visible = false;
			this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
			this.dataGridView1.Controls.Add(this.comboBox);
		}
        private void ShowData()
		{
			this.dataGridView1.RowCount = 4;
			for(int i = 0;i < 4;i++)
			{
				this.dataGridView1.Rows[i].Cells["Column1"].Value = i;
				this.dataGridView1.Rows[i].Cells["Column2"].Value = i+1;
				this.dataGridView1.Rows[i].Cells["Column3"].Value = i+2;
				this.dataGridView1.Rows[i].Cells["Column4"].Value = "";
			}
		}

这时候数据等都已经准备好了jquery给下拉框赋值,连接就是点击单元格显示comboBox。

在comboBox的初始化中,需要添加comboBox事件和comboBox_TextChanged事件。

 		private void ComboBox_TextChanged(object sender, EventArgs e)
		{
			this.dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
			this.comboBox.Visible = false;
		}

jquery给下拉框赋值_刷百度框下拉_jquery下拉分页

然后编译dataGridView的CurrentCellChangedstorm:

		private void DataGridView1_CurrentCellChanged(object sender, EventArgs e)
		{
			try
			{
				if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
				{
					Rectangle rectangle = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
					string value = dataGridView1.CurrentCell.Value.ToString();
					this.comboBox.Text = value;
					this.comboBox.Left = rectangle.Left;
					this.comboBox.Top = rectangle.Top;
					this.comboBox.Width = rectangle.Width;
					this.comboBox.Height = rectangle.Height;
					this.comboBox.Visible = true;
				}
				else
				{
					this.comboBox.Visible = false;
				}
			}
			catch(Exception ex)
			{
				return;
			}
			
		}

因为还需要正常加载时不显示下拉框,选择完成后隐藏下拉框,为此还需要Leave风暴和TextChanged风暴:

		private void ComboBox_Leave(object sender,EventArgs e)
		{
			this.comboBox.Visible = false;
		}
		private void ComboBox_TextChanged(object sender, EventArgs e)
		{
			this.dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
			this.comboBox.Visible = false;
		}

jquery给下拉框赋值_jquery下拉分页_刷百度框下拉

源码已经上传,需要无积分的可以联系我的邮箱。

资源链接: