简体   繁体   中英

Adding button column in datagridview

I have a datagridview which displays sale detail from database. I want to add a delete button at the end of the columns so i can see the delete button at the end of every row. I know to add a button column to datagridview. but here iam displaying data from database and when it displays data i want a custom button column at the end of the gridview. how can we achieve this? I added a button column but it comes at the first position of the datagridview.. after that only my whole data displays. I want it at last position. Can someone help me how to do that?

您可以将DataGridView.Columns[columnName] DisplayIndex属性设置为(列count - 1)以显示最后的按钮列。

dataGridView1.Columns["ButtonColumn"].DisplayIndex = dataGridView1.ColumnCount - 1;

Look at DataGridViewButtonColumn and add it manually to your DataGridView.

DataGridViewButtonColumn dgvButton = new DataGridViewButtonColumn();
myDataGridView.Columns.Add(dgvButton);

And for accessing an Event with Row Data:

myDataGridView.CellContentClick += myDataGridView_CellContentClick;

Calling method:

private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    var senderGrid = (DataGridView)sender;

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {
        //TODO - Your Row Delete Code ;D
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM