您的位置:首页 > 其它

DataGridView 实现一列单元格 显示多操作方式

2016-03-05 21:31 375 查看
RT:

主要使用的方法:

在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: