C# Datagridview Button in single row

Daniele Parasporo 46 Reputation points
2022-08-02T16:07:21.73+00:00

Hello everybody,
I would need some help, I'm creating a program, and through the DataGridView function I get the data from the mysql database, but I need to be able to add buttons (with images) on some lines.
at this moment it adds the button on all lines

Example:
If the event has associated a patient card, it must insert a button, if it has 2 or more patient cards it must insert a different button, if there are no associated patients it must not enter anything

I've googled a lot, but most of the questions in the forums are at least 10 years old.

private void paziente()  
{  
    if (OpenEventMissionData.Rows.Count != 0)  
    {  
        foreach (DataGridViewRow row in OpenEventMissionData.Rows)  
        {  
            string idevento = row.Cells[1].Value.ToString();  
            string sql = "SELECT COUNT(*) FROM paziente WHERE paziente.ID_EVENTO = " + "'" + idevento + "'";  

            MySqlConnection connection = new MySqlConnection();  
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;  
            MySqlCommand cmd = new MySqlCommand(sql, connection);  
            connection.Open();  
            MySqlDataReader reader = cmd.ExecuteReader();  
            reader.Read();  
            int npatient = Convert.ToInt32(reader[0]);  
            //System.Windows.Forms.MessageBox.Show(Convert.ToString(npatient));  
            if (npatient == 1) // Insert button 1  
            {   
                DataGridViewTextBoxColumn image = new DataGridViewTextBoxColumn();  
                OpenEventMissionData.Columns.Add(new PatientColumn());  
            }  
            else if (npatient > 1) // Insert button 2  
            {   
                DataGridViewTextBoxColumn image = new DataGridViewTextBoxColumn();  
                OpenEventMissionData.Columns.Add(new PatientColumn());  
            }  
        }  
    }  
}  

public class PatientCell : DataGridViewButtonCell  
{  
    Image patient = Emergency_Services_Windows_.Properties.Resource.icons8_tipo_di_pelle_utente_7_23;  
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)  
    {  
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);  
        graphics.DrawImage(patient, cellBounds);  
    }  
}  

public class PatientColumn : DataGridViewButtonColumn  
{  
    public PatientColumn()  
    {  
        this.CellTemplate = new PatientCell();  
        this.Width = 20;  
    }  
}
Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-02T17:46:41.53+00:00

    Take a look at the following which does not have images but perhaps you can add them yourself as I don't have time.

    227280-dgvbuttons.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.