C#winform datagridview单元格的单击处理
首先看看效果图:

需求:要求是的在datagridview里面绑定数据后,可以任意点击想要点击的某列的单元格进行改变数据。需要在datagridview里面写3个事件
1.RowPrePaint事件:主要设置要点击的某单元对应的某列显示的颜色
private void dgv_Data_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
try
{
FontStyle newStyle = this.dgv_Data.DefaultCellStyle.Font.Style;
newStyle |= FontStyle.Underline;
Font font = new Font(this.dgv_Data.DefaultCellStyle.Font, newStyle);
foreach (DataGridViewRow dr in this.dgv_Data.Rows)
{
if (dr.Cells["ID"].Value.ToString() == "")
{
dr.DefaultCellStyle.ForeColor = Color.Red;
}
dr.Cells["馆藏重复"].Style.Font = font;
dr.Cells["馆藏重复"].Style.ForeColor = Color.Blue;
dr.Cells["是否采购"].Style.Font = font;
dr.Cells["是否采购"].Style.ForeColor = Color.Blue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2.CellMouseMove事件:主要是鼠标指针移动到单元格时候的样式
private void dgv_Data_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == - || e.RowIndex == -)
{
return;
}
DataGridViewCell cell = this.dgv_Data.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell == null || cell.Value.ToString() == "" || cell.Value.ToString() == "")
{
return;
}
string headText = this.dgv_Data.Columns[e.ColumnIndex].HeaderText;
if (headText == "馆藏重复" || headText == "是否采购")
{
this.Cursor = Cursors.Hand;
}
else
{
this.Cursor = Cursors.Default;
}
}
3.CellClick事件:主要是对单元格进行单击的数据库操作
private void dgv_Data_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == - || e.RowIndex == -)
{
return;
}
DataGridViewCell cell = this.dgv_Data.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell == null || cell.Value.ToString() == "" || cell.Value.ToString() == "")
{
return;
}
Stay_PurchaseData bll = new Stay_PurchaseData();
string headText = this.dgv_Data.Columns[e.ColumnIndex].HeaderText;
switch (headText)
{
case "馆藏重复":
if (cell.Value.ToString().Trim() == "是")
{
bll.ChangeGCCF(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), true);
}
else
{
bll.ChangeGCCF(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), false);
}
break;
case "是否采购":
if (cell.Value.ToString().Trim() == "是")
{
bll.ChangeSFCG(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), true);
}
else
{
bll.ChangeSFCG(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), false);
}
break;
}
//刷新
btn_Query_Click(sender, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
C#winform datagridview单元格的单击处理的更多相关文章
- Winform Datagridview 单元格html格式化支持富文本
Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...
- c# winform DataGridView 单元格的屏幕位置
首先取得DataGridView的坐标位置:int dgvX = dataGridView1.Location.X;int dgvY = dataGridView1.Location.Y;然后取得选中 ...
- Winform DataGridView单元格的提示信息
往往当单元格的内容过多时,显示会变成这样 后缀多了几个点来显示数据未完,当鼠标移到某个单元格时,单元格里的内容会全部显示. 今天偶然发现了一个可以修改提示信息的方法,所以先记下来. 这个方法,可以对于 ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序
TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...
- DataGridView单元格内容自动匹配下拉显示
页面显示数据使用的控件是ComponentFactory.Krypton.Toolkit中的KryptonDataGridView控件.在指定“商品”单元格中需要根据用户输入内容自动匹配数据库中商品信 ...
- DataGridView单元格合并
本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...
- DataGridView单元格显示GIF图片
本文转载:http://home.cnblogs.com/group/topic/40730.html DataGridView单元格显示GIF图片 gifanimationindatagrid.ra ...
- Winfrom设置DataGridView单元格获得焦点(DataGridView - CurrentCell)
设置DataGridView单元格获得焦点 this.dgv_prescription.BeginEdit(true);
随机推荐
- C/C++中的return和exit
return:只是退出函数; ~是关键字. exit:是退出进程; ~是函数名. 整理自:https://www.cnblogs.com/cxchanpin/p/6927025.html
- springBoot项目mybatis中加入缓存
1:maven: <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core --> <dependenc ...
- 区别 |DCL |DDL |DML |DQL
DCL(Data Control Language)数据控制语言: 用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句.这个比较少用到. 对于大多数人,在公司一 ...
- Java环境配置:MacOS
主要是在mac os下进行java环境配置. 下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...
- 59 cuda 不同版本__half冲突问题 —— "__half" has no member "x"
0 引言 深度学习中常常用到half类型的半精度浮点数,但是cpu本身是不支持half的,因此需要进行转换. 1 half - float转换 参考了某博主的github,链接如下. https:// ...
- Core Data could not fulfill a fault
做项目的时候在iOS4系统遇到过这样一个crash,console显示的错误信息是"Core Data could not fulfill a fault". 字面意思是什么?&q ...
- mac 安装并使用 mysql 或者 mac mysql 忘记密码,Can't connect to local MySQL server through socket homebrew
1. brew install mysql 2. 启动mysql mysql.server start 我遇到了这个error,查openstack解决,我在这粘一下 ### Error:Can't ...
- EnhanceFunc__增强函数集
想将经常用到的功能函数写在一起,花时间精心维护,然后以后就用起来就舒服很多了 目前就写了进程调试权限,远程线程注入,远程线程释放这三个函数.还有很多功能,以后慢慢加 // last code by g ...
- class13and14and15_登录窗口
最终的运行效果图(程序见序号6.2): #!/usr/bin/env python# -*- coding:utf-8 -*-# ----------------------------------- ...
- 拾遗:『rhel6系列的开、关机脚本』
一.系统脚本位置及含义 /etc/inittab./etc/init/* 存放开关机配置文件 /etc/init.d/* 服务脚本,是一个到/etc/rc.d/init.d/的软链接 /etc/rc. ...