问题
I want to add two buttons onto datagridview. Now they are on the right side. But I want to locate them on the left.
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.
回答1:
You seems to have design the datagridview from the designer.
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".
You can manage easily in the CellContentClickEvent the column and row which is clicked and then do the job.
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.
回答2:
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.
EditColumn.DisplayIndex = 0;
DelColumn.DisplayIndex = 1;
回答3:
You can't subscribe to the Edit button events directly.
So, the decision to subscribe to the CellClick
event and check which are pressed
And yes, for columns position set DisplayIndex
property
来源:https://stackoverflow.com/questions/10434296/add-buttons-to-datagridview-in-windows-form