DataGridView 实现一列单元格 显示多操作方式
2016-03-05 21:31
375 查看
RT:
主要使用的方法:
在CellPainting绘制操作方式
在CellMouseClick 事件对动作进行处理。
上图:
源码:http://download.csdn.net/detail/zanfeng/9453608
主要使用的方法:
在CellPainting绘制操作方式
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "Op") { Employee model = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as Employee; if (model != null) { StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat; sf.FormatFlags = StringFormatFlags.DisplayFormatControl; sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; sf.Trimming = StringTrimming.EllipsisCharacter; e.PaintBackground(e.CellBounds, false); int md = model.Age % 3; string text = "删除"; if (model.Age % 3 == 1) { text = "修改"; } switch (md) { case 1: e.Graphics.DrawString(text, this.dataGridView1.Font, Brushes.Red, e.CellBounds, sf); break; case 2: string text2 = "删除"; if (model.Age % 3 == 2) { text2 = "修改"; } SizeF size = e.Graphics.MeasureString(text, this.dataGridView1.Font); SizeF size2 = e.Graphics.MeasureString(text2, this.dataGridView1.Font); float z = size.Width / (size.Width + size2.Width); float z2 = size2.Width / (size.Width + size2.Width); RectangleF rect1 = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * z, e.CellBounds.Height); RectangleF rect2 = new RectangleF(rect1.Right, e.CellBounds.Top, e.CellBounds.Width * z2, e.CellBounds.Height); e.Graphics.DrawString(text, this.dataGridView1.Font, Brushes.Red, rect1, sf); e.Graphics.DrawString(text2, this.dataGridView1.Font, Brushes.Red, rect2, sf); break; default: e.Graphics.DrawString(text, this.dataGridView1.Font, Brushes.Red, e.CellBounds, sf); break; } } e.Handled = true; } } }
在CellMouseClick 事件对动作进行处理。
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { Point pt = e.Location; if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "Op") { Employee model = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as Employee; if (model != null) { int md = model.Age % 3; string text = "删除"; if (model.Age % 3 == 1) { text = "修改"; } switch (md) { case 1: break; case 2: string text2 = "删除"; if (model.Age % 3 == 2) { text2 = "修改"; } Graphics g = this.dataGridView1.CreateGraphics(); SizeF size = g.MeasureString(text, this.dataGridView1.Font); SizeF size2 = g.MeasureString(text2, this.dataGridView1.Font); float z = size.Width / (size.Width + size2.Width); float z2 = size2.Width / (size.Width + size2.Width); Rectangle rect = new Rectangle(0, 0, this.dataGridView1.Columns[e.ColumnIndex].Width, this.dataGridView1.Rows[e.RowIndex].Height); RectangleF rect1 = new RectangleF(rect.Left, rect.Top, rect.Width * z, rect.Height); RectangleF rect2 = new RectangleF(rect1.Right, rect.Top, rect.Width * z2, rect.Height); if (rect1.Contains(pt)) { MessageBox.Show("1111111"); } if (rect2.Contains(pt)) { MessageBox.Show("2222222"); } break; default: break; } } } } }
上图:
源码:http://download.csdn.net/detail/zanfeng/9453608
相关文章推荐
- JAVA泛型详解
- 存储过程的使用
- UESTC 1251 谕神的密码【暴力枚举】
- USACO 3.2 fact4 (unc)
- AsyncTask 的注意事项
- Android 弱引用二级缓存实现
- (3) IOS笔记本——UIScrollView的其他应用
- QT环境搭建
- C++中引用与指针的区别(详细介绍)
- 阅读计划与读《构建之法》提出的问题
- POJ2299 Ultra-QuickSort 归并排序求逆序对
- static 和 const
- 【周练2016.3.5】序列的区间操作(对区间的操作,好题)
- 使用maven创建web项目
- 危险系数 割点
- AndoridGridView 自定义适配器 监听事件的使用,实现更换头像功能
- leetcode:Remove Duplicates from Sorted Array 【Java】
- 这些凝视太可爱啦!我也要加到我的项目中去!
- js中如何获取纯正的undefined?
- 常用网站--前端开发类+网页设计类+平面素材类+flash类