I’m not sure I understand your question but I believe you’re looking for the component
Datagridviewbuttoncolumn.
The DataGridViewButtonColumn
is a specialized class of DataGridViewColumn
used to logically host cells that respond to simple user input. A DataGridViewButtonColumn
has associated a DataGridViewButtonCell
in each DataGridViewRow
that intersects on it. Each cell provides a user interface (UI) that is similar to a control Button
.
To reset the text on the button of each cell, set the property UseColumnTextForButtonValue
as true
and adjust the property Text
for the text of the desired button.
Since you didn’t put any sample of your code, I have to create a fictional example and it’s your discretion to adapt it to your reality.
In this example I will add an object of the type DataGridViewButtonColumn
called bColumn1
to an object of the type DataGridView
called dataGridView1.
DataGridViewButtonColumn bColumn1 = new DataGridViewButtonColumn();
bColumn1.Name = "bColumn1";
bColumn1.Text = "Remover Linha";
int columnIndex = 0;
if (dataGridView1.Columns["bColumn1"] == null)
{
dataGridView1.Columns.Insert(columnIndex, bColumn1);
}
I create an event to manipulate the click.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["bColumn1"].Index)
{
// Aqui vai o código para apagar sua linha do Banco de Dados.
// A propriedade e.RowIndex determina a linha a qual foi clicada.
}
}
Adding the event CellClick
.
dataGridView1.CellClick += dataGridView1_CellClick;
To place the image on each button you manipulate the event DataGridView.CellPainting
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e){
if (e.ColumnIndex == 0){
// Você pode usar e.RowIndex para decidir qual a imagem usada por cada linha
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
var w = Properties.Resources.suaImage.Width;
var h = Properties.Resources.suaImage.Height;
var x = e.CellBounds.Left + (e.CellBounds.Width - w) / 2;
var y = e.CellBounds.Top + (e.CellBounds.Height - h) / 2;
e.Graphics.DrawImage(Properties.Resources.suaImage, new Rectangle(x, y, w, h));
e.Handled = true;
}
}
Add the event CellPainting
.
dataGridView1.CellPainting += dataGridView1_CellPainting;
Good morning! Actually it would not be a button more an image on the phone, I will use as button, more with the image I can put something that describes the field, already with the button I have no space to put the name of the field. In the photo that you add in the body of the message you have field in the dataGridView the first, it only gets that image.
– Jamisson Ramos
I made an edit adding image on buttons.
– Augusto Vasques