sup3r93

Member
Joined
Aug 1, 2022
Messages
8
Location
Milano, Italy
Programming Experience
Beginner
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.

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;
            }
        }
 
Solution
That might work, but is not a very good design. If you are going to customize a column to essentially hold a different data type or control, then you might as well do it the right way like you were doing previously. The only major thing you seemed to be missing there was setting the state of each grid cell, and having your custom grid cell paint accordingly based on the state.

Take time to read the DataGridView documentation. The How-To part for customizing a data grid view column will help a lot. Also look for any tutorials or posts by Fred Bird about customizing the data grid view. He was very helpful when I was learning to deal with the DGV.

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
I've googled a lot, but most of the questions in the forums are at least 10 years old.
That is correct. The DataGridView has been around since .NET Framework 2.0, and when everyone was into WinForms.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
Your lines 20 and 25 are not doing anything.

Are lines 21 and 26 correctly adding the column(s)?

Set a breakpoint on line 37 to see if your code is being called, and inspect the variables to see where the image is being painted to.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
at this moment it adds the button on all lines
That's because your Paint on line 35 makes no distinction about the which row it is currently painting on.

Be aware that adding a column to a data grid view (your lines 21 and 26) applies for all the rows, not just the rows that made you determine that you needed some extra columns.
 

sup3r93

Member
Joined
Aug 1, 2022
Messages
8
Location
Milano, Italy
Programming Experience
Beginner
Your lines 20 and 25 are not doing anything.

Are lines 21 and 26 correctly adding the column(s)?

Set a breakpoint on line 37 to see if your code is being called, and inspect the variables to see where the image is being painted to.
Yes points 21 and 26 add the column
2.png

1.png
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
But is Winform still usable or is it not recommended?
WinForms was originally supposed to be at end-of-life with the original .NET Core project having no intentions of supporting it. Microsoft did a turn around with .NET Core 3 and re-injected new life into WinForms by porting it into the .NET Core 3 and higher. (I suspect it is because too many line of business apps were floating around in companies all over the world that Microsoft caved in and had to continue to support it.)
 

sup3r93

Member
Joined
Aug 1, 2022
Messages
8
Location
Milano, Italy
Programming Experience
Beginner
I still can't figure out how to do it, I thought about creating a DataGridViewTextBoxColumn and changing the cells by inserting a button, but I don't know. 😭
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
That might work, but is not a very good design. If you are going to customize a column to essentially hold a different data type or control, then you might as well do it the right way like you were doing previously. The only major thing you seemed to be missing there was setting the state of each grid cell, and having your custom grid cell paint accordingly based on the state.

Take time to read the DataGridView documentation. The How-To part for customizing a data grid view column will help a lot. Also look for any tutorials or posts by Fred Bird about customizing the data grid view. He was very helpful when I was learning to deal with the DGV.
 
Solution
Top Bottom