Enable Images in Datagridview

Asked

Viewed 251 times

-1

Good morning, you guys!

I’m new here, sorry if I’m posting my question in the wrong place. More let’s go there.

I have a form that has a Datagridview, where this grid has the fields that was made by the assistant of own . My question is why I wanted to add a column that would receive an icon that would be my Bhutan to delete the record. Icon was successfully placed, but when I load the frm does not appear the icon I put, and another error appears. I’m using visual studio C# 2017 inserir a descrição da imagem aqui

1 answer

0


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 DataGridViewButtonColumncalled 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.

  • I made an edit adding image on buttons.

Browser other questions tagged

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