C# 向DataGridView添加命令列
我有一个带有一些列的c#DataGridView。我想添加一个Action列作为每行的最后一个单元格C# 向DataGridView添加命令列,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我有一个带有一些列的c#DataGridView。我想添加一个Action列作为每行的最后一个单元格 每行都应该有查看、删除、更新等按钮或图标。以下是我可以想到的几个选项: 到目前为止,最简单的方法是为每个按钮添加一列。(推荐!) 当然,您可以创建一个自定义单元格类型,可能是一个带有所需按钮的面板子类。这涉及到使用至少十几个字段和方法实现IDataGridViewEditingControliterface,以及从DataGridViewColumn和DataGridViewCell派生的两个
每行都应该有查看、删除、更新等按钮或图标。以下是我可以想到的几个选项:
- 到目前为止,最简单的方法是为每个按钮添加一列。(推荐!)
- 当然,您可以创建一个自定义单元格类型,可能是一个带有所需按钮的
子类。这涉及到使用至少十几个字段和方法实现面板
iterface,以及从IDataGridViewEditingControl
和DataGridViewColumn
派生的两个自定义类,还有许多事情要做。简而言之,工作量巨大!对于一个复杂的编辑控件来说,这可能是值得的。对于一些DataGridViewCell
当然不是!(不推荐!)按钮
- 或者你可以在
事件中,通过一个小主人的画法伪造CellPainting
按钮。见下文李>
- 或者,您可以在作用于当前行的
外部添加一个sopisticated控件。通常的方式李>DataGridView
- 或者,您可以在作用于当前行的
DatagGridView-DGV
的第4列中绘制了四个'SIDU'命令:
private void Form1_Load(object sender, EventArgs e)
{
DGV.Rows.Add(12);
for( int i = 0; i< DGV.Rows.Count; i++)
{
DGV[0, i].Value = i;
DGV[1, i].Value = R.Next(1000);
DGV[2, i].Value = rights[R.Next(rights.Count)];
DGV[3, i].ReadOnly = true;
}
}
List<string> rights = new List<string>
{ "SIDU", "SID-", "SI-U", "S-DU", "SI--", "S--U", "S-D-", "S---" };
Dictionary<char, string> rightsTexts = new Dictionary<char, string>
{ { 'S', "Select" }, { 'I', "Insert" }, { 'D', "Delete" }, { 'U', "Update" } };
Random R = new Random(1);
private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
string r = DGV[2,e.RowIndex].Value.ToString();
StringFormat format = new StringFormat()
{ LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center};
int w = e.CellBounds.Width / 4;
int y = e.CellBounds.Y + 1;
int h = e.CellBounds.Height - 2;
e.PaintBackground(e.CellBounds, false);
for (int i = 0; i < 4; i++)
{
int x = e.CellBounds.X + i * w;
Rectangle rect = new Rectangle(x, y, w, h);
ControlPaint.DrawButton(e.Graphics, rect, ButtonState.Normal);
if (rightsTexts.ContainsKey(r[i]))
e.Graphics.DrawString(rightsTexts[r[i]], DGV.Font,
SystemBrushes.WindowText, rect ,format );
}
e.Handled = true;
}
}
private void DGV_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];
int w = cell.Size.Width;
int buttonIndex = e.X * 4 / w;
Text = rightsTexts.ElementAt(buttonIndex).Value;
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
DGV.Rows.Add(12);
对于(int i=0;i=0)
{
字符串r=DGV[2,e.RowIndex].Value.ToString();
StringFormat格式=新的StringFormat()
{LineAlignment=StringAlignment.Center,Alignment=StringAlignment.Center};
int w=e.cellbunds.Width/4;
int y=e.CellBounds.y+1;
int h=e.CellBounds.Height-2;
e、 油漆背景(如蜂窝边界,假);
对于(int i=0;i<4;i++)
{
int x=e.CellBounds.x+i*w;
矩形rect=新矩形(x,y,w,h);
ControlPaint.DrawButton(例如图形、rect、ButtonState.Normal);
if(rightsTexts.ContainsKey(r[i]))
e、 Graphics.DrawString(rightsTexts[r[i]],DGV.Font,
SystemBrusks.WindowText、rect、format);
}
e、 已处理=正确;
}
}
私有void DGV_CellMouseClick(对象发送方,DataGridViewCellMouseEventArgs e)
{
如果(e.ColumnIndex==3&&e.RowIndex>=0)
{
DataGridViewCell cell=DGV[e.ColumnIndex,e.RowIndex];
int w=单元大小宽度;
int按钮索引=e.X*4/w;
Text=rightsTexts.ElementAt(buttonIndex).Value;
}
}
画画的东西被抛出而不是q&d,所以你可以投入更多的精力来微调它
我已选择在可见单元格中显示权限以供演示。对于生产,动作单元格的值将是明显的位置。您必须为每个命令添加一列类型为DataGridViewButtonColumn
。这里和那里有很多示例。。可能重复感谢,但我认为我的问题有点不同,我需要添加多个按钮,即一个按钮面板(如更新、删除、编辑、查看)我怎样才能添加一个按钮面板,每个按钮都有不同的动作。不,你不可能轻松做到这一点。你不能向单元格中添加控件。你可以启动或创建自定义单元格类型,但要准备为几个简单的按钮做大量的工作!显然不值得我这么做。为每个按钮和你的优点添加一列!这里有一句话:DataGridView控件提供多种列类型,使用户能够以多种方式输入和编辑值。但是,如果这些列类型不满足您的数据输入需要,您可以使用所选控件所在的单元格创建自己的列类型。为此,您必须定义从DataGridViewColumn和DataGridV派生的类您还必须定义一个从控件派生并实现IDataGridViewEditingControl接口的类。