winfrom datagridview中DataGridViewTextBoxColumn的联动处理
这个问题有两种方法 第一种是用DataGridview中自带的DataGridViewTextBoxColumn 控件,第二种是动态添加combobox控件
方法一:
首先 窗体上拖拽一个 DataGridview
然后在这个DataGridview中添加两列DataGridViewTextBoxColumn (第一列叫A,第二列叫B)
然后绑定A代码
A.DataSource = ds.Tables[].DefaultView;
A.DisplayMember = "table_name";
A.ValueMember = "table_name";
((DataGridViewComboBoxColumn)dataGridView1.Columns[]).DefaultCellStyle.NullValue = "--请选择--"; //默认值
其次是绑定B代码
//当前选中行的第二列赋值
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[]).DataSource = ds.Tables[].DefaultView;
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[]).DisplayMember = "comments";
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[]).ValueMember = "column_name";
((DataGridViewComboBoxColumn)dataGridView1.Columns[]).DefaultCellStyle.NullValue = "--请选择--";
然后添加SelectedIndexChanged事件代码
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.CurrentCell.OwningColumn.Name == "A")
{
ComboBox cb = (ComboBox)e.Control;
cb.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
SelectedIndexChanged事件代码
public void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (dataGridView1.CurrentCell.OwningColumn.Name == "表名")
{
if (comboBox.Text != "")
{
//这是绑定B的方法
DBFieldNote(comboBox.Text);
}
}
}
方法二:
首先实例化combobox对象
private ComboBox comboBox = new ComboBox();
private ComboBox cb = new ComboBox();
其次:
this.dataGridView1.Controls.Add(comboBox);//将控件添加到DataGridview中
DBTableName();//绑定comboBox
comboBox.Visible = false;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);//添加事件
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string name = ((ComboBox)sender).Text;
dataGridView1.CurrentCell.Value = name;//将选中的值添加到DataGridview当前选中的单元格里
DBFieldNote(name);//绑定第二个combobox(也就是cb)
}
public void DBFieldNote(string tablename)
{
this.dataGridView1.Controls.Add(cb);
cb.Visible = false;
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
......
}
private void cb_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = cb.Text;
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell != null)
{
if (dataGridView1.CurrentCell.ColumnIndex == )//如果选中的是第一列 就显示第一个combobox
{
System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex ,false);//获取当前选中的单元格的属性(宽 ,高等)
comboBox.Left = rect.Left;
comboBox.Top = rect.Top;
comboBox.Width = rect.Width;
comboBox.Height = rect.Height;
comboBox.Visible = true;
}
else if (dataGridView1.CurrentCell.ColumnIndex==)//如果是选中第二列就显示cb
{
System.Drawing.Rectangle rect1 = dataGridView1.GetCellDisplayRectangle(comboxIndex + , dataGridView1.CurrentCell.RowIndex, false);
cb.Left = rect1.Left;
cb.Top = rect1.Top;
cb.Width = rect1.Width;
cb.Height = rect1.Height;
cb.Visible = true;
}
}
else
{
comboBox.Visible = false;
}
}
winfrom datagridview中DataGridViewTextBoxColumn的联动处理的更多相关文章
- Winfrom DataGridView中使用Tooltip
第一步:添加DataGridView.Tooltip控件,略 第二步:设置ToolTip 相关属性,略,参考下图 第三步:DataGridView 添加 CellMouseEnter.CellMous ...
- DataGridView 中添加CheckBox和常用处理方式 .
DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...
- Datagridview 添加checkbox列,并判断Datagridview 中的checkbox列是否被选中
Solution1://In Fill DataGridViewEvent : DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxC ...
- 将listBox中信息显示在dataGridview中,操作datagridview后删除listBox信息和SQL数据库信息 续(浅谈listBox..)
应用场景 对datagridview控件使用了解,以及操作datagridview选中的信息删除,并且有二次确认后才删除用户信息.相应的删除listbox中用户信息,下面一起看看需要哪些准备 ...
- 在Datagridview中添加datagridviewComboBox列并显示下拉列表
在DataGridView中自动的添加Column. private void button_autoAddColumn_Click(object sender, EventArgs e) { try ...
- [Winform] DataGridView 中 DataGridViewComboBox 的可编辑
在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...
- 禁用datagridview中的自动排序功能
把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...
- C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel
其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...
- DataGridView中实现checkbox全选的自定义控件
在DataGridView中实现Checkbox的全选的方法就是在列头画一个checkbox, 并给其一个事件. 这个之前很多blog都有写, 这里就不多废话了, codeproject上面有示例代 ...
随机推荐
- vue-cli3.0 使用postcss-plugin-px2rem(推荐)和 postcss-pxtorem(postcss-px2rem)自动转换px为rem 的配置方法;
如何在vue-cli3.0中使用postcss-plugin-px2rem 插件 插件的作用是 自动将vue项目中的px转换为rem . 为什么这三个中要推荐 postcss-plugin-px2r ...
- Redis创建集群报错
Redis创建集群报错: 1:任何一个集群节点中都不能存在数据,如果有备份一下删除掉aof文件或rdb文件 2: nodes-集群端口.conf 文件存的会有报错记录,所以该文件也要删除
- C++ Primer 笔记——类
1.定义在类内部的函数是隐式的inline函数. 2.因为this的目的总是指向“这个”对象,所以this是一个常量指针,我们不允许改变this中保存的地址. 3.常量成员函数:允许把const关键字 ...
- 饮冰三年-人工智能-Python-12之利其器pycharm
1:下载.安装.与激活这里不再介绍了.需要注意的是激活时需要修改C:\Windows\System32\drivers\etc\hosts文件 2:工具使用 2.1 HelloWorld File& ...
- 步步為營-98-MyAPI
1 通过NuGet程序管理包添加 Microsoft Asp.Net webAPI 2.2 的引用 2 添加两个文件夹Controllers和Models 2.1 在本地模拟数据库,所以在Model ...
- C#操作windows服务
本文主要说明了Windows服务的安装.卸载.启动.停止.获取服务安装路径.获取服务状态.检查服务是否存在.获取服务版本. 我们需要引用命名空间using System.Configuration.I ...
- 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分+线段树区间合并
题解: 好像和noi那题并没有什么区别 只是加上了修改和变成树上 比较显然我们可以用树链剖分来维护
- P2860 [USACO06JAN]冗余路径Redundant Paths
题解: 首先要边双缩点这很显然 然后变成树上问题 发现dp,dfs好像不太对 考虑一下度数 发现只要在度数为1的点之间连边 但我好像不太会证明这个东西.. 网上也没有看到比较正确的证明方法和连边策略. ...
- C# 之 日常问题积累(一)
1.响应在此上下文中不可用(Response). 异常:响应在此上下文中不可用 产生异常的过程:在asp.net程序中添加了一个 类.cs ,其中有一段代码如下 : Response.ClearCon ...
- shell判断文件是否为空
[[ `cat a.log |wc -l` -eq 0 ]] && echo "file is empty"