Kiro Coneski
Kiro Coneski

Reputation: 515

How to get the instance of a button that is inside a DataGridView cell?

I'm not sure if this is possible, but I want to get the instance of a button that is inside a DataGridView cell. (Working with c# .net and windows forms)

The reason of this is because I want to popup a panel with controls near that button, so I need it's location. (Is this possible?).

I'm handling the click of the button in an CellClicked event handler:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        Int32 id = (Int32)dataGridView1[0, e.RowIndex].Value;
        if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["NewDate"].Index)
        {
            if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Delete"].Index) return;
            else
            {

            }
        }
        else
        {
            //Button button - the button I'm trying to get 
            panel2.Location=new Point(button.Location.X,button.Location.Y);

        }

    }

Upvotes: 1

Views: 722

Answers (1)

Nikola Davidovic
Nikola Davidovic

Reputation: 8666

You don't have to get the button but the Rectangle of the clicked cell. Check the code:

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        Int32 id = (Int32)dataGridView1[0, e.RowIndex].Value;
        if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["NewDate"].Index)
        {
            if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Delete"].Index) return;
            else
            {

            }
        }
        else
        {
            Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
            panel2.Location=new Point(rect.X,rect.Y);

        }

    }

Upvotes: 1

Related Questions