NicoHormann
NicoHormann

Reputation: 11

how to add buttons to a datagridview and be able to scroll up and down?

When I add buttons to a datagridview and then scroll up or down, buttons remain static and do not scroll together with the datagridview.

This is the code I have for the button click event:

    private void button4_Click(object sender, EventArgs e)
    {
        button5.Location = new Point(dgvTurne.HitTest(posicionBoton.X, posicionBoton.Y).ColumnX, dgvTurne.HitTest(posicionBoton.X, posicionBoton.Y).RowY);
        Size size = new Size(dgvTurne.SelectedCells[0].Size.Width, 0);
        foreach (DataGridViewCell celda in dgvTurne.SelectedCells)
        {
            size.Height += celda.Size.Height;
        }
        button5.Size = size;
        dgvTurne.Controls.Add(this.Controls["button5"]);
    }

In the event above, when a button is clicked, I get the position of the selection in the datagridview by creating an instance of the Point class. Coordenates are taken using HitTest method from datagridview. Then the button is added to datagridview.

The problem is that after button is added, when I scroll up or down, button remains static and do not scroll together with datagridview.

I am trying this: Each button will contain information about appointments. For example: "Appointment with dentist"

enter image description here

Upvotes: 1

Views: 697

Answers (2)

OhBeWise
OhBeWise

Reputation: 5454

Disclaimer:

Edits to the OP invalidated this answer. This will add a button for each row in a single column, but by the new image posted, this may not completely meet the needs of the original question. It won't extend a button over multiple rows like for the time slots imaged above.

See Octopoid's answer.


It sounds like you want a button associated with each row. Manually adding buttons and moving their location is far too much work. What you want to do is add a column of buttons to the dgv. After you've bound your DataSource, add a button column like so:

DataGridViewButtonColumn btnCol = new DataGridViewButtonColumn();
btnCol.HeaderText = "Foo";
btnCol.Name = "Bar";
btnCol.Text = "Click Me";
btnCol.UseColumnTextForButtonValue = true;
this.dataGridView1.Columns.Add(btnCol);
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(col_Click);

Then, to handle the click event for each button, you handle the CellClick event.

public void col_Click(object sender, DataGridViewCellEventArgs e)
{
  if (e.ColumnIndex == this.dataGridView1.Columns["Bar"].Index)
  {
    // Whatever you want the button to do
  }
}

Upvotes: 0

Octopoid
Octopoid

Reputation: 3698

I warn you now- getting this working perfectly will most likely ruin your brain. I know it did mine.

Creating a custom DataGridView with all the various events and missing events and everything else caught and handled is FAR too complicated to cover in this answer, but the starting point is here:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetScrollPos(IntPtr hWnd, int nBar);

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0115) // WM_VSCROLL
    {
        int scrollPos = MyDataGridView.GetScrollPos(this.Handle, 1); // SB_VERT
        // get the vertical scroll position

        this.button1.Y = desiredY + scrollPos;
        // you'll need to calculate where all the buttons should be, then offset them by the scroll pos
    }

    base.WndProc(ref m);
}

That should cover scrolling with the wheel, grabbing the bar, pressing the arrow buttons next to the bar, using up, down, home and end on the keyboard and any other ways I might have forgotten.

Once that's done though you'll likely find another situation that's not properly covered, or it's not redrawing properly, or any number of other issues and it's back to the Win32 messages again..

Upvotes: 1

Related Questions