Andre
Andre

Reputation: 11

Adding buttons in relation to row

I've trying to add two button to the rows that have value 2, but these button appear only in relation to the dataGridView.

If I don't give them a specific location they will appear at the top of the dataGridView, and when I give them a specific location they will still appear in relation to the dataGridView.

How can I make them appear in relation to each row that has value 2?

I want to make the buttons appear at the bottom or inside that specific row. I've been looking for an answer for a long time and I can't find anything.

Button buttonOk = new Button() { Width = 100, Height = 50 };
Button buttonCancel = new Button() { Width = 100, Height = 50 };

    public void method1(DataGridView dataGridView1)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {

            if ((string)row.Cells[2].Value == ("1"))
            {
                row.DefaultCellStyle.ForeColor = Color.Green;
            }

            else if ((string)row.Cells[2].Value == ("2"))
            {
                row.DefaultCellStyle.ForeColor = Color.Blue;
                dataGridView1.Controls.Add(buttonOk);
                dataGridView1.Controls.Add(buttonCancel);   
                //buttonOk.Location = new Point(00, 74);
            }
            else if ((string)row.Cells[2].Value == ("3"))
            {
               //do something
            }
        }
    }

Upvotes: 1

Views: 57

Answers (1)

MikeH
MikeH

Reputation: 4395

You can't add buttons to a DataGridView. You can, however, define a column as a DataGridViewButtonColumn. Of course the problem is that the button will show in all the rows. You could implement a custom DataGridViewButtonColumn as shown in this MSDN thread. With your custom button column you can now hide the buttons as desired.

Upvotes: 1

Related Questions