Add buttons to the Datagridview cell

Asked

Viewed 3,512 times

2

I am developing a Windows Form C# activity control application for a company, however I would need to display activities such as buttons within the cell of GridView.

I’ve tried to:

DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.UseColumnTextForButtonValue = true;
uninstallButtonColumn.HeaderText = "Delete/Edit";
uninstallButtonColumn.Name = "uninstall_column";
uninstallButtonColumn.Text = "Teste";

dgvPrincipal.Columns.Insert(1, uninstallButtonColumn);

This format allows only 1 button, when in fact there can be several buttons on the same cell.

And I’ve tried too:

dgvPrincipal.Rows[0].Cells[0].DataGridView.Controls.Add(tbnTeste);

This inserts the button without any link with the cells..

Does anyone know a way? Like DataRowBound... or something like that?

  • Why don’t you put the buttons in another column?

  • At this link has an example that can give you a direction to follow.

1 answer

2

In my case it solved my problem...

You can only use one DataGridViewImageColumn and use the event DataGridViewCellEventArgs to verify which cell was clicked.

Obs: In my case, I was needing two buttons on my DataGridView, one to edit the selected content and one to delete the item from selected line.

  • Step 1: You should add 2 columns in your Grid, and set them as DataGridViewImageColumn. Add the required images to each field.
  • Step 2: Go to the events of Grid and double-click on the field
    CellContentClick or CellClick. Whatever one or the other the difference is that the first will only be used if the user click on the image inside the cell. The second will work when the user click on the whole cell.
  • Step 3: At the event of DataGridView, write the code below:

    inserir a descrição da imagem aqui

I hope I’ve helped...

If it helped you, don’t forget to mark the answer as correct.

Big Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.