首先看看效果图:

需求:要求是的在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单元格的单击处理的更多相关文章

  1. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

  2. c# winform DataGridView 单元格的屏幕位置

    首先取得DataGridView的坐标位置:int dgvX = dataGridView1.Location.X;int dgvY = dataGridView1.Location.Y;然后取得选中 ...

  3. Winform DataGridView单元格的提示信息

    往往当单元格的内容过多时,显示会变成这样 后缀多了几个点来显示数据未完,当鼠标移到某个单元格时,单元格里的内容会全部显示. 今天偶然发现了一个可以修改提示信息的方法,所以先记下来. 这个方法,可以对于 ...

  4. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  5. WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序

    TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...

  6. DataGridView单元格内容自动匹配下拉显示

    页面显示数据使用的控件是ComponentFactory.Krypton.Toolkit中的KryptonDataGridView控件.在指定“商品”单元格中需要根据用户输入内容自动匹配数据库中商品信 ...

  7. DataGridView单元格合并

    本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...

  8. DataGridView单元格显示GIF图片

    本文转载:http://home.cnblogs.com/group/topic/40730.html DataGridView单元格显示GIF图片 gifanimationindatagrid.ra ...

  9. Winfrom设置DataGridView单元格获得焦点(DataGridView - CurrentCell)

    设置DataGridView单元格获得焦点 this.dgv_prescription.BeginEdit(true);

随机推荐

  1. systemctl命令的使用及服务状态的查看

    二.systemctl命令 systemctl list-units            ##列出当前系统服务的状态 systemctl list-unit-files       ##列出服务的开 ...

  2. VS项目种类GUID

    在VS里新建的类库项目,在添加新建项时往往找不到模板.比如想新建一个WPF的资源词典文件,VS认为该类库项目不是WPF类型,就没有列出新建资源词典的模板.解决办法是在csproj文件的<Prop ...

  3. webpack4.0基础

    安装 yarn add webpack webpack-cli -D npx webpack index.js 图片 file-loader module: { rules: [ { test: /\ ...

  4. leetcode-168周赛-1296-划分数字为连续数字的集合

    题目描述: 自己的提交: class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: c = col ...

  5. python 实现九九乘法表

    代码如下: # for九九表 for i in range(1,10): for j in range(1,10): if j<=i: print('{}*{}={}'.format(i,j,i ...

  6. NX二次开发-UFUN重命名工程图UF_DRAW_rename_drawing

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize ...

  7. Ehcache3.x学习(一)入门

    简介 Ehcache 是一个开源的高性能缓存,拥有很高的拓展性和伸缩性,广泛使用各种 Java 项目中(如 Hibernate 默认使用 Ehcache作为二级缓存),在目前基于 Java 的缓存方案 ...

  8. 20140309 C++ using 野指针 返回变量首地址

    1.C++中的using:http://blog.sina.com.cn/s/blog_61e904fd0100nuk3.html 使用using恢复.改变被继承类中的访问权限 2.野指针,没有指向的 ...

  9. PAT L2-021. 点赞狂魔 /// sort+unique去重

    https://www.patest.cn/contests/gplt/L2-021 题目大意: 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每篇博文都有一些刻画其特性的标签,而你点赞 ...

  10. IRP FLAGS

    IRP所有标识位的含义,是 _IRP . flags 这个成员 IRP_NOCACHE 0x00000001 //表示I/O请求从存储的媒介而不是高速缓存中读取数据 IRP_PAGING_IO 0x0 ...