Add buttons to datagridview in windows form

折月煮酒 提交于 2019-12-11 06:26:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!