sup3r93
Member
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.
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.
C#:
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;
}
}