Search code examples
c#datagridviewpicturebox

How to still be able to click on the cell of dataGridView even when you have a picturebox on the cell?


I have a dataGridView where I display the production process. On the right cell of the dataGridView that is called Is Roll Ready, I make the visibility of the combobox which offers Yes and No options (i.e. I created when initializing) when this cell is clicked.

enter image description here

If Yes is clicked, I create a pictureBox with a tick image and if No is clicked I create a pictureBox with a cross icon like below:

enter image description here

enter image description here

Once the cell is clicked I assign my global PictureBox picture to the pictureBoxthat is added to the clicked cell and I make use of Click() event of pictureBox to make the combobox visible when the pictureBox (hence the cell) is clicked. Below is my CellClick event for the datagridview:

  private void CompletedMachines_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1) return;
            if (CompletedMachines.Columns[e.ColumnIndex].Name.CompareTo("MachineNr2") != 0 && CompletedMachines.Columns[e.ColumnIndex].Name.CompareTo("TimePassedSince") != 0 &&
                 CompletedMachines.Columns[e.ColumnIndex].Name.CompareTo("NrOfLinesPerCm2") != 0 && e.RowIndex > -1 && CompletedMachines.Columns[e.ColumnIndex].Name.CompareTo("HeightOfLamallae2") != 0)
            {
                
                PictureBox pictureBox = new PictureBox();
                picture = pictureBox; //assign the newly created pictureBox to the global one              CompletedMachines.Controls.Add(pictureBox);
                cellRect = CompletedMachines.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                comboBox.Size = new Size(cellRect.Width, cellRect.Height);
                comboBox.Location = new Point(cellRect.X, cellRect.Y);
                comboBox.Visible = true;
                pictureBox.Size = new Size(cellRect.Width, cellRect.Height);
                pictureBox.Location = new Point(cellRect.X, cellRect.Y);
                pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox.Click += PictureBox_Click; //call the Click event
            }
        }

PicutreBox Click event:

   private void PictureBox_Click(object sender, EventArgs e)
        {
            
            comboBox.Visible = true;
        }

However, when I have multiple rows in the dataGridView, every time this PictureBox_Click event is called, the combobox appears on the cell that owns the latest added picturebox. This is because I assign the global picturebox picture to the latest added one.

Is there a way to still make use of CellClick event of the dataGridView even if a custom control is added on the cell so that I do not have to use PictureBox_Click event? Or any other way?

Thanks in advance!


Solution

  • Add reference to ComboBox in PictureBox.Tag.

    // When creating PictureBox
    pictureBox.Tag = comboBox;
    

    And then use sender in event handler to get PictureBox and ComboBox:

    private void PictureBox_Click(object sender, EventArgs e)
    {
      var comboBox = (ComboBox)((PictureBox)sender).Tag;          
      comboBox.Visible = true;
    }