添加链接列
如图:
//1.添加一个超链接列,并控制其大小不可改变,固定大小
DataGridViewLinkColumn dgvlcOprate = new DataGridViewLinkColumn();
dgvlcOprate.HeaderText = "操作";
dgvlcOprate.Width = 120;
dgvlcOprate.Resizable = DataGridViewTriState.False;//设置此列不可调整大小
dgvlcOprate.Name = "oprate";
dgvData.Columns.Add(dgvlcOprate);
//2.设置单元格中连接的操作
dgvData.Rows[index].Cells["oprate"].Value = "查看" + " | " + "编辑" + " | " + "删除";
//3.获取鼠标在单元格中点击的位置
int mouseX = 0;
int mouseY = 0;
private void dgvData_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex > -1)
{
if (dgvData.Columns[e.ColumnIndex].Name == "oprate")
{
mouseX = e.X;
mouseY = e.Y;
}
}
}
//4.在点击当前单元格时,鼠标点击的位置和单元格的宽度比较,控制点击的位置,算出点击的是哪里,控制点击的事件
private void dgvData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
switch (dgvData.Columns[e.ColumnIndex].Name)
{
case "oprate":
Rectangle rc = dgvData.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);//单元格区域的矩形
if (0 < mouseX && mouseX < rc.Width / 3)
{
MessageBox.Show("查看" + e.RowIndex + "行" + e.ColumnIndex + "列");
}
else if (rc.Width / 3 < mouseX && mouseX < ((rc.Width / 3) * 2))
{
MessageBox.Show("编辑" + e.RowIndex + "行" + e.ColumnIndex + "列");
}
else if (((rc.Width / 3) * 2) < mouseX && mouseX < rc.Width)
{
MessageBox.Show("删除" + e.RowIndex + "行" + e.ColumnIndex + "列");
}
break;
}
}
}
--------------------- 作者:林子的传智 来源:CSDN 原文:https://blog.csdn.net/linzi1015910507/article/details/52595863?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!