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);
随机推荐
- [翻译]windows下 连接到 bitnami的phpmyadmin
bitnami 因为安全考虑,只能 localhost 访问 phpmyadmin 为了能通过SSH 隧道访问 phpMyAdmin,你需要一个ssh 客户端.参考文章介绍使用中选择使用 PuTTY, ...
- shell 例子
shell编程入门 http://www.runoob.com/linux/linux-shell-variable.html http://c.biancheng.net/cpp/shell/ .查 ...
- 【PKUWC2018】猎人杀
题目描述 题目分析 设\(W=\sum\limits_{i=1}^nw_i\),\(A=\sum\limits_{i=1}^nw_i[i\ is\ alive]\),\(P_i\)为下一个打中\(i\ ...
- SolidWorks直线命令快捷转换为圆弧命令
在进行草图绘制的时候,有时候需要切换到圆弧命令,此时来回切换比较麻烦, 我们可以将鼠标回碰线段起点,此时便成为了圆弧工具. 再次回碰,可改变圆心方向 利用鼠标操作,快捷切换绘图工具.
- php上传(二)
上传的主体页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- FastText总结,fastText 源码分析
文本分类单层网络就够了.非线性的问题用多层的. fasttext有一个有监督的模式,但是模型等同于cbow,只是target变成了label而不是word. fastText有两个可说的地方:1 在w ...
- Java设计模式简单总结
1.单例模式:在应用程序整个生命周期中,单例类的实例只有一个,并且会自动实例化.单例类的构造方法必须为私有,并且提供一个全局访问点 public class Test { private Test() ...
- Java-Class-@I:org.junit.Test
ylbtech-Java-Class-@I:org.junit.Test 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 1. package org.junit; import ...
- jquery click事件失效
除了最基本的语法错误,还可能是因为,元素根本点击不到. z-index:99;
- MongoDB点滴
0 http://blog.csdn.net/mydeman/article/details/6652387 1 MongoDB 内置连接池,不需要使用额外的连接池驱动 Note: The Mongo ...