C# 以windows形式向datagridview添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10434296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add buttons to datagridview in windows form
提问by
I want to add two buttons onto datagridview. Now they are on the right side. But I want to locate them on the left.
我想在 datagridview 上添加两个按钮。现在他们在右边。但我想把它们放在左边。
The other thing is I want to add an update event for "Edit" button. Is it
另一件事是我想为“编辑”按钮添加一个更新事件。是吗
private void Edit_Click(object sender, EventArgs e)
{
}
Form code:
表格代码:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'qDataSet.Metric' table.
// You can move, or remove it, as needed.
this.metricTableAdapter.Fill(this.qDataSet.Metric);
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.Text = "Edit";
EditColumn.Name = "Edit";
EditColumn.DataPropertyName = "Edit";
dataGridView1.Columns.Add(EditColumn);
DataGridViewButtonColumn DelColumn = new DataGridViewButtonColumn();
DelColumn.Text = "Delete";
DelColumn.Name = "Delete";
DelColumn.DataPropertyName = "Delete";
dataGridView1.Columns.Add(DelColumn);
}
The image likes:

图片喜欢:

Thank you.
谢谢你。
采纳答案by ykatchou
You seems to have design the datagridview from the designer.
您似乎已经从设计师那里设计了 datagridview。
You can use the wizard which allow to edit columns. There you'll add two columns (one for editing and the other for deleting). You can choose where you want to set those columns. The text of the button is defined using the property "Text", you can also set the property "UseColumnTextForButton".
您可以使用允许编辑列的向导。在那里您将添加两列(一列用于编辑,另一列用于删除)。您可以选择要设置这些列的位置。按钮的文本使用属性“Text”定义,您也可以设置属性“UseColumnTextForButton”。
You can manage easily in the CellContentClickEvent the column and row which is clicked and then do the job.
您可以在 CellContentClickEvent 中轻松管理被点击的列和行,然后完成工作。
If you want to manage access right (like allowing someone to editing and someone else to not editing) you can play with show/hide of this column.
如果您想管理访问权限(例如允许某人编辑而其他人不编辑),您可以显示/隐藏此列。
回答by SwDevMan81
See DisplayIndex
The zero-based position of the column as it is displayed in the associated DataGridView, or -1 if the band is not contained within a control.
列在关联 DataGridView 中显示时的从零开始的位置,如果带不包含在控件中,则为 -1。
EditColumn.DisplayIndex = 0;
DelColumn.DisplayIndex = 1;
回答by Eugene
You can't subscribe to the Edit button events directly.
So, the decision to subscribe to the CellClickevent and check which are pressed
您不能直接订阅编辑按钮事件。因此,决定订阅CellClick事件并检查按下了哪些
And yes, for columns position set DisplayIndexproperty
是的,对于列位置设置DisplayIndex属性

