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);
随机推荐
- Aira搭建
Aira下载地址 https://github.com/aria2/aria2/releases AriaNg下载地址 https://github.com/mayswind/AriaNg/relea ...
- leetcode -有效的字母异位词 python&C++
C++解题代码: class Solutiion { public: bool isAnagram(string s, string t) { ](); int n = s.length(); int ...
- 【JZOJ6403】a
description analysis 考虑\((0,0,0)\)走到某个点\((i,j,k)\)的贡献,相当于插板问题 \(i+j\)个空插\(k\)个板可以有空,\(i\)个空插\(j\)个板可 ...
- BASS HOME
http://www.un4seen.com/
- thinkphp 表单合法性检测
在处理表单提交的数据的时候,建议尽量采用Think\Model类提供的create方法首先进行数据创建,然后再写入数据库. 大理石平台厂家 create方法在创建数据的同时,可以进行更为安全的处理操作 ...
- NX二次开发-对话框解锁UF_UI_unlock_ug_access
VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...
- python从入门到大神---4、python3文件操作最最最最简单实例
python从入门到大神---4.python3文件操作最最最最简单实例 一.总结 一句话总结: python文件操作真的很简单,直接在代码中调用文件操作的函数比如open().read(),无需引包 ...
- delphi基础篇之项目文件
delphi基础篇之项目文件 program Teacher2018; uses Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pa ...
- 实现solr热词排行榜
现在有业务场景,要求实现词库里面,最新,最热的词并显示,点击热词后可以进入相关信息的文章或者句子 热词的显示频率12小时更新一次. 实现思路: 实现步骤:
- input输入内容成可点击状态
<!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1. ...