C#在dataGridView中动态添加一个combox

liu_ing 2015-06-16 09:42:24
急求大神!!我在dataGridVIew1的某一列中动态添加一个combox,然后出现下面的情况:



...全文
1077 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liu_ing 2015-06-16
  • 打赏
  • 举报
回复
DataGridViewComboBoxCell cm = new DataGridViewComboBoxCell(); DataGridViewColumn newcolumn = new DataGridViewColumn(); 怎样把列的单元格弄成 DataGridViewComboBoxCell??查资料,找不到。。 然后是把该列添加到指定的列中吗??怎么弄啊。。我还是菜鸟。。
皮特胖胖 2015-06-16
  • 打赏
  • 举报
回复
引用 4 楼 liu_ing 的回复:
[quote=引用 3 楼 hironpan 的回复:] DataGridView中的组合框为DataGridViewComboBoxCell,动态添加很简单,参考以下代码

private void Button1_Click(object sender, EventArgs e)
{
	DataGridViewComboBoxCell newComboBoxCell = new DataGridViewComboBoxCell();
	newComboBoxCell.Items.AddRange({
		"选项1",
		"选项2",
		"选项3"
	});
	DataGridViewRow newRow = new DataGridViewRow();
	newRow.Cells.Add(newComboBoxCell);
	DataGridView1.Rows.Add(newRow);
}
这是给行添加combox的。。可我只想要给指定的一列添加[/quote] 方法1、用DataGridViewComboBoxColumn 方法2、同以上代码,可以在任意行的任意列添加
liu_ing 2015-06-16
  • 打赏
  • 举报
回复
引用 3 楼 hironpan 的回复:
DataGridView中的组合框为DataGridViewComboBoxCell,动态添加很简单,参考以下代码

private void Button1_Click(object sender, EventArgs e)
{
	DataGridViewComboBoxCell newComboBoxCell = new DataGridViewComboBoxCell();
	newComboBoxCell.Items.AddRange({
		"选项1",
		"选项2",
		"选项3"
	});
	DataGridViewRow newRow = new DataGridViewRow();
	newRow.Cells.Add(newComboBoxCell);
	DataGridView1.Rows.Add(newRow);
}
这是给行添加combox的。。可我只想要给指定的一列添加
皮特胖胖 2015-06-16
  • 打赏
  • 举报
回复
DataGridView中的组合框为DataGridViewComboBoxCell,动态添加很简单,参考以下代码

private void Button1_Click(object sender, EventArgs e)
{
	DataGridViewComboBoxCell newComboBoxCell = new DataGridViewComboBoxCell();
	newComboBoxCell.Items.AddRange({
		"选项1",
		"选项2",
		"选项3"
	});
	DataGridViewRow newRow = new DataGridViewRow();
	newRow.Cells.Add(newComboBoxCell);
	DataGridView1.Rows.Add(newRow);
}
liu_ing 2015-06-16
  • 打赏
  • 举报
回复
引用 楼主 liu_ing 的回复:
急求大神!!我在dataGridVIew1的某一列中动态添加一个combox,然后出现下面的情况:
为什么会出现这种情况啊??各位大神!!是这个函数出错了吗?? private void dataGirdView1_cellEnter(object sender, DataGridViewCellEventArgs e){ }
liu_ing 2015-06-16
  • 打赏
  • 举报
回复
下面是我的代码: public ComboBox mycombo = new ComboBox();//全局变量 ....form_load(...) { mycombo.Name = "mycombo"; mycombo.Items.Clear(); mycombo.Items.Add("三甲"); mycombo.Items.Add("二甲"); mycombo.Items.Add("一甲"); mycombo.Items.Add("三乙"); mycombo.Items.Add("二乙"); mycombo.Items.Add("一乙"); mycombo.Items.Add("三丙"); mycombo.Items.Add("二丙"); mycombo.Items.Add("一丙"); mycombo.TextChanged += new EventHandler(Mycombo_changed); / /为生成的组合框控件,添加文本改变事件处理函数 dataGridView1.Controls.Add(mycombo); //把组合框添加到dataGridView1中 } //当文本改变时,把dataGridView当前单元格内容设为组合框的内容 private void Mycombo_changed(object sender, EventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex == 3) dataGridView1.CurrentCell.Value = mycombo.Text; } //为dataGridView控件的单元格获得焦点事件 //即当医院等级列的单元格获得焦点时,显示生成的组合框 private void dataGirdView1_cellEnter(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) { mycombo.Visible = false; mycombo.Left = dataGridView1.GetCellDisplayRectangle(3, dataGridView1.CurrentRow.Index, true).Left;//横坐标 mycombo.Top = dataGridView1.GetCellDisplayRectangle(3, dataGridView1.CurrentRow.Index, true).Top;//纵坐标 mycombo.Text = dataGridView1.CurrentCell.Value.ToString() + " ";//组合框的文本为单元格的值 mycombo.Visible = true;//组合框显示 } else { mycombo.Visible = false; } } //单击dataGridView控件,组合框隐藏 private void dataGridView1_Click(object sender, EventArgs e) { mycombo.Visible = false; }
Imcx 2015-06-16
  • 打赏
  • 举报
回复
引用 6 楼 liu_ing 的回复:
DataGridViewComboBoxCell cm = new DataGridViewComboBoxCell(); DataGridViewColumn newcolumn = new DataGridViewColumn(); 怎样把列的单元格弄成 DataGridViewComboBoxCell??查资料,找不到。。 然后是把该列添加到指定的列中吗??怎么弄啊。。我还是菜鸟。。
this.dataGridView1.Columns.Add(cm);这样

110,556

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧