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上面有示例代 ...
随机推荐
- Data Preprocess
本文试图解决一个问题,即我们自定义的数据如何训练模型?初识深度学习,我们接触手写数字识别模型,但是批次数据是mnist已经定义好的,我们现在有自己的图片如何做成批次进行训练模型. 现在我们将准备好的原 ...
- MYSQL之 error while loading shared libraries: libtinfo.so.5: cannot open shared objectfile: No such f
环境:ubuntu18 登陆MYSQL时遇到错误:mysql: error while loading shared libraries: libtinfo.so.5: cannot open sha ...
- 三维拓扑排序好题hdu3231
/* 三维拓扑排序 将每个长方体分解成六个面,xyz三维进行操作 每一维上的的所有长方体的面都应该服从拓扑关系,即能够完成拓扑排序=如果两个长方体的关系时相交,那么其对应的三对面只要交叉即可 如 a1 ...
- Nginx详解二十八:Nginx架构篇Nginx+Lua的安全waf防火墙
Nginx+Lua的安全waf防火墙 看一下别人写好的:https://github.com/loveshell/ngx_lua_waf 先安装git:yum -y install git 在/opt ...
- jQuery常见的几个文档处理方式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- RabbitMQ在java中基础使用
RabbitMQ相关术语: 1.Broker:简单来说就是消息队列服务器实体. 2.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列. ...
- Python练习题
内置函数 # 5.随意写一个20行以上的文件# 运行程序,先将内容读到内存中,用列表存储.# 接收用户输入页码,每页5条,仅输出当页的内容 def user_check(filename,num=5) ...
- 饮冰三年-人工智能-linux-08 软件包管理(Python的安装)
1:软件包存放的位置 media/CentOS_6.9_Final/Packages文件夹下 2.RPM就是Red Hat Package Manger(红帽软件包管理工具)的缩写. 2.1 常用的命 ...
- 滴水穿石-07Java开发中常见的错误
1:使用工具Eclipse 1.1 "语法错误" 仅当源级别为 1.5 时已参数化的类型才可用 设置eclipse,窗口—>java—>编译器—>JDK一致性调到 ...
- 用C语言实现窗口抖动
#include "stdafx.h" #include <stdio.h> #include<Windows.h> int main() { ; //休眠 ...