Answered Adding different buttons to rows datagridview

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
Is it possible to have custom buttons on different rows using Datagridview? I manage to have each button in two separate columns. But not in the same one.
How can this be done? I can post the current code I'm using if needed.

1613545456056.png
 
There are no Button controls in the first place. You handle the CellContentClick event of the grid and then it's up to you to decide what do then. If you want to do different things for different rows then do so. If you want different values in different rows then set the Value of the cells to whatever you want displayed.
 
I'm using DataGridViewButtonColumn already. But only gets one type of button. Would like one that says "Buy toy" and one "Play" so I can target each one.

This is what my code for creating the "Buy button" looks like, which creates the first image in #1.

C#:
DataTable dt = new DataTable();
dataGridView1.DataSource = dt;

DataGridViewButtonColumn buttonPlay = new DataGridViewButtonColumn();
{
    buttonBuy.Name = "button";
    buttonBuy.HeaderText = "Button";
    buttonBuy.Text = "Buy toy";
    buttonBuy.UseColumnTextForButtonValue = true; //dont forget this line
}

this.dataGridView1.Columns.Add(buttonBuy);
 
The UseColumnTextForButtonValue property allows you to either set the button text for each cell, or to use the Text property value for all of the button cells.
This means you have to turn off UseColumnTextForButtonValue if you want different button texts, and set each cell Value to the texts you want.
 
Back
Top Bottom