本人要自定义一个DataGridViewColumn,实现一个单元格中有两个linklable,实现如WEB数据列表页面的"删除、修改"功能
本人不想用合并单元格的做法,那样做太麻烦,工程量大,需要修改的地方也很多。做成组件会比较好。但想不出如何实现。
DataGridViewLinkColumn好像只能实现一个link,请问多个link如何实现呢?谢谢了。
本人不想用合并单元格的做法,那样做太麻烦,工程量大,需要修改的地方也很多。做成组件会比较好。但想不出如何实现。
DataGridViewLinkColumn好像只能实现一个link,请问多个link如何实现呢?谢谢了。
那个link是画上去的。
要做的话,可能你要向你头家申请几天的工作量,而不是几个小时的工作量。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;namespace WindowsApplication151
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); DataGridView DGV = new DataGridView();
DGV.Parent = this;
DGV.Columns.Add(new TwoLinkButtonColumn());
DGV.Rows.Add(3);
} class TwoLinkButtonColumn : DataGridViewColumn
{
public TwoLinkButtonColumn()
{
this.CellTemplate = new TwoLinkButtonCell();
}
} class TwoLinkButtonCell : DataGridViewCell
{
public TwoLinkButtonCell()
{
this.ValueType = typeof(String);
} protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
Point CurrentMousePosition = this.DataGridView.PointToClient(Control.MousePosition);
Size ButtonSize = new Size(cellBounds.Size.Width / 2, cellBounds.Size.Height);
Font ButtonFont = new Font("宋体", 10, FontStyle.Underline); Point Button1Location = cellBounds.Location;
Point Button2Location = Point.Add(cellBounds.Location, new Size(cellBounds.Size.Width / 2, 0)); Rectangle Button1Bounds = new Rectangle(Button1Location, ButtonSize);
Rectangle Button2Bounds = new Rectangle(Button2Location, ButtonSize); String Button1Text = "更新";
String Button2Text = "取消"; bool Button1Down = Button1Bounds.Contains(CurrentMousePosition)
&& (Control.MouseButtons == MouseButtons.Left);
bool Button2Down = Button2Bounds.Contains(CurrentMousePosition)
&& (Control.MouseButtons == MouseButtons.Left); graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds); ButtonRenderer.DrawButton(graphics, Button1Bounds, Button1Text, ButtonFont, false,
Button1Down ? PushButtonState.Pressed : PushButtonState.Normal);
ButtonRenderer.DrawButton(graphics, Button2Bounds, Button2Text, ButtonFont, false,
Button2Down ? PushButtonState.Pressed : PushButtonState.Normal);
} protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
Point CurrentMousePosition = this.DataGridView.PointToClient(Control.MousePosition);
Rectangle CellBounds = this.DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
Point CellLocation = CellBounds.Location;
Size ButtonSize = new Size(CellBounds.Size.Width / 2, CellBounds.Size.Height); Point Button1Location = CellLocation;
Point Button2Location = Point.Add(CellLocation, new Size(CellBounds.Size.Width / 2, 0)); Rectangle Button1Bounds = new Rectangle(Button1Location, ButtonSize);
Rectangle Button2Bounds = new Rectangle(Button2Location, ButtonSize); bool Button1Down = Button1Bounds.Contains(CurrentMousePosition)
&& (e.Button == MouseButtons.Left);
bool Button2Down = Button2Bounds.Contains(CurrentMousePosition)
&& (e.Button == MouseButtons.Left); if (Button1Down)
MessageBox.Show("更新 行:" + e.RowIndex.ToString());
else if (Button2Down)
MessageBox.Show("取消 行:" + e.RowIndex.ToString());
}
}
}
}