1

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: buttons

Thank you.

Community
  • 1
  • 1

4 Answers4

6

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.

ykatchou
  • 3,667
  • 1
  • 22
  • 27
1

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;
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • I guess that it is 0,1 rather than 1,2. Then how about event code? –  May 03 '12 at 15:21
  • @Love - See [here](http://stackoverflow.com/questions/3577297/how-to-handle-click-event-in-button-column-in-datagridview) and [here](http://stackoverflow.com/a/8909611/95573) – SwDevMan81 May 03 '12 at 15:24
0

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

Eugene
  • 1,699
  • 1
  • 12
  • 13
0
DataGridViewButtonColumn ButtonColumn = new DataGridViewButtonColumn();
ButtonColumn.Name = "Print";
ButtonColumn.HeaderText = "Print";

ButtonColumn.FlatStyle = FlatStyle.Popup;
ButtonColumn.DefaultCellStyle.ForeColor = Color.White;
ButtonColumn.DefaultCellStyle.BackColor = Color.CadetBlue;
ButtonColumn.Text = "Print";
ButtonColumn.UseColumnTextForButtonValue = true;
int columnIndex = 12; /*your column index number*/
if (dtGridTicket.Columns["Print"] == null)
{
    dtGridTicket.Columns.Insert(columnIndex, ButtonColumn);
}

If you load the page column index hide so you use

dtGridTicket.Columns.RemoveAt(12);
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
AligNoor
  • 1
  • 1