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);
随机推荐
- 浏览器http跳转至https问题
Chrome 浏览器 地址栏中输入 chrome://net-internals/#hsts 在 Delete domain security policies 中输入项目的域名,并 Delete 删 ...
- (转载)openwrt nginx
ln -s ../../../feeds/packages/net/fcgiwrap/ fcgiwrap 本帖最后由 cjd6568358 于 2018-6-4 11:21 编辑 刚又把路由器重置重新 ...
- ps使logo背景色透明
方法一:魔法工具(对复杂的logo误差较大) 魔法工具--左键点击选区--delete--保存 方法二:拾色器 1.有的站上的素材图片不能直接用,需要先变成rgb图像,可这样操作:图像\模式,选择rg ...
- 高危预警|RDP漏洞或引发大规模蠕虫爆发,用户可用阿里云免费检测服务自检,建议尽快修复
2019年9月6日,阿里云应急响应中心监测到Metasploit-framework官方在GitHub空间公开了针对Windows远程桌面服务远程命令执行漏洞(CVE-2019-0708)的利用代码. ...
- 优化剪枝搜索——牛客多校第二场F
试了很多种爆搜和剪枝,最后发现还是状压的比较好用 #include <bits/stdc++.h> using namespace std; // #define IO #define f ...
- Kafka Streams详细
概述 1 Kafka Streams Kafka Streams.Apache Kafka开源项目的一个组成部分.是一个功能强大,易于使用的库.用于在Kafka上构建高可分布式.拓展性,容错的应用程序 ...
- NX二次开发-NXOpen中Point3d类型转换成point类型
NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...
- mysql key分区,分区数制定
我相信不 太注意的同学肯定会入坑,今天我差点也入坑了,后面自己问自己如果我用key分区,自己问自己 我的分区数应该是多少??? 后面我陷入了沉思......... 我第一次想先随便弄一个分区数,在本地 ...
- Reverses CodeForces - 906E (最小回文分解)
题意: 给你两个串s和t,其中t是由s中选择若干个不相交的区间翻转得到的,现在要求求出最少的翻转次数以及给出方案. 1≤|s|=|t|≤500000 题解: 我们将两个字符串合成成T=s1t1s2t2 ...
- 【POJ】1860 Currency Exchange
真是气skr人..没把d[]换成double...de了一上午的bug// 记得用G++提交啊 题目链接:http://poj.org/problem?id=1860 题意:告诉你n个点,m条路.起始 ...