winform中切换下拉框值改变事件改变dgv中数据

1034人阅读
Winform(42)
设置单击下拉模式(否则需要点两下才能显示下拉框):
DataGridView属性 EditMode 为 EditOnEnter &
EditMode属性
EditOnEnter
当单元格接收到焦点时即可开始编辑。在按 Tab 键在行中横向输入值,或按 Enter 键在列中纵向输入值时,此模式非常有用。&
当单元格获得焦点时按 F2 开始编辑。此模式将选择点放置在单元格内容的末尾。&
EditOnKeystroke
当单元格获得焦点时按任意字母数字键开始编辑。&
EditOnKeystrokeOrF2
当单元格获得焦点时按任意字母数字键或 F2 开始编辑。&
EditProgrammatically
仅在调用&&方法时开始编辑。 &
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:398986次
积分:4868
积分:4868
排名:第4454名
原创:28篇
转载:403篇
评论:43条
(2)(11)(5)(2)(2)(5)(5)(3)(5)(7)(4)(22)(1)(2)(14)(14)(14)(7)(7)(1)(3)(5)(14)(3)(8)(33)(10)(3)(14)(13)(8)(16)(5)(3)(6)(6)(8)(6)(5)(12)(15)(6)(24)(14)(39)(28)c# winform
datagridview如何用代码添加一列? - 开源中国社区
当前访客身份:游客 [
当前位置:
&无详细内容&
共有1个答案
<span class="a_vote_num" id="a_vote_num_
//来个表,手动的表就是好!!
var d= new DataTable();
//想加几个列就加几个列!
d.Columns.Add(&a&);
d.Columns.Add(&b&);
d.Columns.Add(&c&);
//相加几个行就加几个行
//每行可以满员,也可以个别字段没有值(比如这里的c)
var r = d.NewRow();
r[&a&] = 1;
r[&b&] = 2;
d.Rows.Add(r);
//数据的类型也可以随意整,不限制
var r2 = d.NewRow();
r2[&a&] = &wakaka&;
r2[&c&] = 2;
d.Rows.Add(r2);
//绑定上去
dataGridView1.DataSource =
--- 共有 3 条评论 ---
: Delphi 的tlist是可以直接这样增加key,val的.
(3年前)&nbsp&
: 貌似不能这样吧,不过写法这些个东西都是虚的,你特别愿意这样的话,适当的封装一下,就可以这么写了(无非是个字符串解析)
(3年前)&nbsp&
d.Columns.Add("a=xxxx");
不能直接这样?
(3年前)&nbsp&
更多开发者职位上
有什么技术问题吗?
xuli201...的其它问题
类似的话题主要需求为:界面中有两个DataGridView分别命名为:dgvParent(绑定主表)和dgvChild(绑定子表).当选中dgvParent时,dgvChild中显示其对应的Child数据.(这个实现没什么难度).但当选中dgvChild时,需要在dgvParent选中对应的Father数据.因为在dgvParent中有Selecti>C#语言
--------------------------------------
???????没看明白,你为啥会这么想?既然主触发子显示,那么子显示的必定就是主显示触发后的结果。无论你在子表里干啥,都已经是主表那个选项的子结果,根本就不存在还需要根据子结果反向选择主表的操作
--------------------------------------
因为我子表有查询,比如说我查询出了全部子表的数据,当选中子表对应的一条数据,对应主表就选中,可以理解为:我们一个学校很多个班,而每个班里面有很多学生.对应班就是主表,而学生就是子表,当我们在子表中查询出了"张三"这个学生,可能会有100个学生叫"张三",当我们选中一个张三,我们在主表中就选择他是属于哪个班的学生.Winform的DataGridView下拉框事件(可实现列的关联操作)
一、、、、、
Winform的DataGridView下拉框事件(可实现列的关联操作)
这是今天用到的一些东西,DataGridView中有两列是DataGridViewComboBoxColumn类型的,要用第一个下拉列表控制第二个,有点类似二级联动的效果,在这里记录一下。
首先要在DataGridView的EditingControlShowing事件中给下拉框动态添加事件,如下:
复制内容到剪贴板
private void dataGridView1_EditingControlShowing(object
sender,DataGridViewEditingControlShowingEventArgs e)
//给指定列的下拉框添加SelectedIndexChanged事件
if (dataGridView1.CurrentCell.RowIndex != -1
dataGridView1.CurrentCell.ColumnIndex == 1)
((ComboBox)e.Control).SelectedIndexChanged += new
EventHandler(v2_SelectedIndexChanged);
在这里就可以写事件了
复制内容到剪贴板
private void v2_SelectedIndexChanged(object sender, EventArgs
//这里写要处理的代码。。。最后要把事件删除,不然会出问题
((ComboBox)sender).SelectedIndexChanged -= new
EventHandler(v2_SelectedIndexChanged);
另外一种思路实现操作:
窗体加载里为datagridview加上两个combobox列,并从机构数据表里读取数据绑定机构列。
private void insert_Load(object sender, EventArgs e) {
DataGridViewCheckBoxColumn zuzhang = new DataGridViewCheckBoxColumn();
zuzhang.HeaderText =
DataGridViewComboBoxColumn jigou = new DataGridViewComboBoxColumn();
jigou.HeaderText =
DataGridViewComboBoxColumn jianchayuan = new DataGridViewComboBoxColumn();
jianchayuan.HeaderText ="检查员";
dataGridView2.Columns.Add(zuzhang);
dataGridView2.Columns.Add(jigou);
dataGridView2.Columns.Add(jianchayuan); dataGridView2.Rows.Add();
SqlConnection conn =
new SqlConnection(); conn.ConnectionString
= cqchz_mis.Connection.C
string cmdstr2; cmdstr2 = "select * from jigou";
conn.Open(); SqlCommand cmd2 = new SqlCommand(cmdstr2, conn); SqlDataAdapter
adpt2 = new SqlDataAdapter(); adpt2.SelectCommand
= cmd2; DataSet ds2 = new DataSet(); adpt2.Fill(ds2, "jigou");
jigou.DisplayMember =
"机构名称";
jigou.ValueMember =
"机构代码";
jigou.DataSource =
ds2.Tables["jigou"];
conn.Close(); }
我通过datagridveiw的CellValueChanged事件来动态绑定人员列。
private void dataGridView2_CellValueChanged(object
sender, DataGridViewCellEventArgs
(e.ColumnIndex == 1)
{ if (e.RowIndex & -1)
{ if (dataGridView2.Rows[e.RowIndex].Cells[1].Value
== System.DBNull.Value) { return; } DataGridViewComboBoxCell cell
= dataGridView2.Rows[e.RowIndex].Cells[2]
as DataGridViewComboBoxCell; SqlConnection
conn = new SqlConnection(); conn.ConnectionString
= cqchz_mis.Connection.C
string cmdstr2; cmdstr2 = "select 检查员编号,检查员姓名 from jianchayuan where
所属机构代码 = ‘"
+ dataGridView2.Rows[e.RowIndex].Cells[1].Value
conn.Open(); SqlCommand cmd2 = new SqlCommand(cmdstr2, conn); SqlDataAdapter
adpt2 = new SqlDataAdapter(); adpt2.SelectCommand
= cmd2; DataSet ds2 = new DataSet(); adpt2.Fill(ds2, "jianchayuan");
cell.DataSource =
ds2.Tables["jianchayuan"];
cell.DisplayMember =
"检查员姓名";
cell.ValueMember =
"检查员编号";
conn.Close(); } } }
二、、、、、、
Google了一下,
原来这个问题很多人碰到过,也有一些解决方案.不过感觉大多不好,有些许的问题.也许有好的,我没找到吧.于是在前人基础上,小小的改动了下.
目前还有两个小问题待解决:
DataGridView最后一行,第一列的ComboBox输入值,DataGridView应该自动增加一行(编辑其他列会自动增加一行).
2) 第一列的ComboBox中,假设其Items中有Shenzhen, 我输入Shen,Cell中的Value将会时Shen.
当再次点击ComboBox,显示下拉Item时,Item中的Shenzhen会高亮显示,ComboBox的Text会自动变成Shenzhen.
其实这也是ComboxBox的一个功能.可能有时不需要这个功能.
System.Windows.F
//此Form中有两个控件,一个DataGridView(dataGridView1),一个ComboBox(comboBox1)
//DataGridView中有两列(都是DataGridViewTextBoxColumn的),其中第一列实现类似ComboBox的功能
//第一列中只有正在编辑的Cell才会显示ComboBox,其他的不会显示,这样好看一点
public partial class Form1 : Form
private int comboBoxColumnIndex = 0; // DataGridView的首列
public Form1()
InitializeComponent();
InitComboBoxValues();
this.dataGridView1.Controls.Add(this.comboBox1);
this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(dataGridView1_CellEnter);
this.dataGridView1.CellLeave+=new DataGridViewCellEventHandler(dataGridView1_CellLeave);
private void InitComboBoxValues()
this.comboBox1.Items.AddRange(new String[] { "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Shenzhen" });
this.comboBox1.AutoCompleteMode = AutoCompleteMode.S //输入提示
this.comboBox1.AutoCompleteSource
= AutoCompleteSource.ListI
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
if (e.ColumnIndex == comboBoxColumnIndex)
//此处cell即CurrentCell
DataGridViewCell cell
= this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect =
this.dataGridView1.GetCellDisplayRectangle(cell.ColumnIndex,
cell.RowIndex, true);
this.comboBox1.Location = rect.L
this.comboBox1.Size = rect.S
comfirmComboBoxValue(this.comboBox1, (String)cell.Value);
this.comboBox1.Visible = true;
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
if (e.ColumnIndex == comboBoxColumnIndex)
//此处cell不为CurrentCell
DataGridViewCell cell
= this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value =
this.comboBox1.T
this.comboBox1.Visible = false;
private void comfirmComboBoxValue(ComboBox com, String
cellValue)
com.SelectedIndex =
if (cellValue == null)
com.Text =
com.Text =
foreach (Object item in com.Items)
if ((String)item == cellValue)
com.SelectedItem =
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 winform 下拉框多选 的文章

 

随机推荐