C# 关于dgv中DataGridViewComboBoxCell触发事件
cell触发事件的先后顺序:
①不进行cell编辑情况时:
CellEnter-->CellLeave-->CellValidating-->CellValidated
②对cell进行编辑后:
CellEnter-->CellBeginEdit-->CellLeave-->CellValidating-->CellValueChanged-->CellValidated-->CellEndEdit
1.从数据库中读取下拉列表数据
/// <summary>
/// 新添加行时显示下拉框
/// </summary>
private void dgvShowCombobox()
{
//传感器小类
DataGridViewComboBoxCell dgvSnrStyleCombox = new DataGridViewComboBoxCell();
string sqlSensorType = "SELECT sensorType,sensorStyle FROM sensorstyle";
MySqlDataAdapter cbxAdapter = MySQLConnect.GetMySQLDB().getAdapter(sqlSensorType);
DataSet dtSensorTypeCombox = new DataSet();
cbxAdapter.Fill(dtSensorTypeCombox, "sensorstyle");
dgvSnrStyleCombox.DataSource = dtSensorTypeCombox.Tables["sensorstyle"];
dgvSnrStyleCombox.DisplayMember = "sensorStyle";
dgvSenserManage.Rows[dgvSenserManage.Rows.Count - 1].Cells[2] = dgvSnrStyleCombox;
}
2.下拉框改变时触发事件
#region 对dgv中的下拉框操作
/// <summary>
/// dgv中DataGridViewComboBoxCell编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvSenserManage_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
//判断相应的列
if (dgv.CurrentCell.GetType().Name == "DataGridViewComboBoxCell" && dgv.CurrentCell.RowIndex != -1)
{
//给这个DataGridViewComboBoxCell加上下拉事件
(e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
/// <summary>
/// 组合框事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combox = sender as ComboBox;
combox.Leave += new EventHandler(combox_Leave);
try
{
//在这里就可以做值是否改变判断
if (combox.SelectedItem != null)
{
//MessageBox.Show("在这里做获取选中值赋值到大类中的操作", "提示");
int currentColumn = (int)dgvSenserManage.CurrentCell.ColumnIndex;
if (currentColumn == 2) //传感器小类
{
String snrStyleCurrentValue = ((DataRowView)(combox.SelectedItem)).Row.ItemArray.GetValue(0).ToString();
string sql = string.Format("SELECT sensorType FROM sensorstyle WHERE sensorStyle='{0}'", snrStyleCurrentValue);
MySqlCommand command = MySQLConnect.GetMySQLDB().giveCommand(sql);
//StringmysqlStr="Database=GuGong;DataSource=127.0.0.1;UserId=root;Password=123456;pooling=false;CharSet=utf8;port=3306";//连接数据库是注意设置编码
//MySqlConnection mysql = new MySqlConnection(mysqlStr);
//MySqlCommand mysqlCommand = new MySqlCommand(sql, mysql);
//mysql.Open();
//MySqlDataReader reader = command.ExecuteReader();
//string snrType = "";
//while (reader.Read())
//{
// if (reader.HasRows)//有一行读一行
// {
// snrType = (string)reader["sensorType"];
// }
//}
//dgvSenserManage.CurrentRow.Cells["sensorType"].Value = snrType;
object obj = command.ExecuteScalar();
if (obj != null)
{
String snrType = obj.ToString();
dgvSenserManage.CurrentRow.Cells["sensorType"].Value = snrType;
}
}
if(currentColumn == 3) //厂家全称
{
}
}
//Thread.Sleep(100);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 离开combox时,把事件删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void combox_Leave(object sender, EventArgs e)
{
ComboBox combox = sender as ComboBox;
//做完处理,须撤销动态事件
combox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
}
#endregion
3.在此单元格失去焦点后单元格内值更改触发事件
/// <summary>
/// 单元格中的值改变时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvSenserManage_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1) return;
if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
DataGridViewComboBoxCell cc;
if (e.ColumnIndex == 2) //ComboBoxCell列传感器小类
{
cc = (DataGridViewComboBoxCell)this.dgvSenserManage.CurrentCell;
if (cc.Value != null)
{
string dgvCell = Convert.ToString(dgvSenserManage.CurrentCell.Value);
MessageBox.Show(dgvCell);
}
}
}
C# 关于dgv中DataGridViewComboBoxCell触发事件相关推荐
- JavaScript中的触发事件
这两天在敲订餐管理系统的界面的时候,才感觉到自己在前端这里才疏学浅,什么都没有整理过,因为页面的效果想使用JavaScript来展示,却发现不知道JS的代码是如何被驱动的,因此才有了这篇博客,帮助大家 ...
- html事件中写js,html标签中绑定触发事件与js中绑定触发事件写法上的区别
html> js中方法名后加括号和不加口号的区别 按钮 var test = function(){ var span = document.createElement("span&q ...
- fragment中listview触发事件setOnItemClickListener不好使
<listView/>中// listview点击 ,高度wrap_content改成fill_prarent 转载于:https://www.cnblogs.com/zhujiabin/ ...
- [unreal4入门系列之十四] 在UE4中添加碰撞触发事件
一.在HUD中显示消息 1) 在MyHUD.h中定义一个结构来表示我们的消息: #pragma once #include "GameFramework/HUD.h" #inclu ...
- oracle 触发器的种类和触发事件
触发器的种类和触发事件 触发器必须由事件才能触发.触发器的触发事件分可为3类,分别是DML事件.DDL事件和数据库事件. 每类事件包含若干个事件,如下所示.数据库的事件是具体的,在创建触发器时要 ...
- MVVM下拉框绑定触发事件
开发工具与关键技术:MVVM 撰写时间:2021年06月15日 在MVVM中下拉框的绑定跟MVC的下拉框绑定基本一样,只不过在MVVM中要写多一步声明下拉框的属性来接收数据. 首先我们要在DAL层写查 ...
- oracle触发器 触发事件,oracle 触发器的种类和触发事件
oracle触发器的种类和触发事件,DML触发器,DDL事件触发器,替代触发器,查看触发器 触发器的种类和触发事件 触发器必须由事件才能触发.触发器的触发事件分可为3类,分别是DML事件.DDL事件和 ...
- 在C#中使用代理的方式触发事件
事件(event)是一个非常重要的概念,我们的程序时刻都在触发和接收着各种事件:鼠标点击事件,键盘事件,以及处理操作系统的各种事件.所谓事件就是由某个对象发出的消息.比如用户按下了某个按钮,某个文件发 ...
- DataList中的按钮触发事件的方法的实现
DataList中的按钮触发事件的方法的实现 1.要在CommandName中设定名字如: <asp:Button id="Button1" runat=&quo ...
最新文章
- 25个让Java程序员更高效的Eclipse插件
- Linux下的vim编辑器与gcc编译器及静动态库的制作
- linux静态编译libcurl,libcurl嵌入式Linux移植
- 实际编程题----CT扫描
- linux mysql5.1 安装_linux编译安装mysql5.1.x
- PHP基础学习之数组使用要点
- 直线检测原理matlab,Hough变换 直线检测原理及其Matlab实现
- SaxReader解析xml文件--绕过多层for循环
- 客户端的gzip解压
- iOS中雷达波扩散Layer层动画的实现
- 兼容微信字体变大,页面错乱问题
- html类型的网页文件,文件的ContentType类型-网页设计,HTML/CSS
- uc浏览器返回按钮的兼容_浏览器的返回、后退、上一页按钮的事件方法
- 人像摄影长焦镜头拍摄攻略
- 14_扫雷游戏地雷数计算
- centos系统中php Curl 无法访问https 的解决办法
- 报错Can‘t resolve ‘crypto‘ in ‘E:\test\jwt-demo-master\frontend\node_modules\jwa‘
- js 运行中断停止_如何终止 JS 脚本的运行
- 小米能使用鸿蒙,小米、oppo、VIVO ……手机友商到底谁愿支持鸿蒙?
- 萝卜魂军曹机器人_《萝卜魂》作品详介第6弹:《闪亮的塔科特》