且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在DataGridView的列中添加按钮

更新时间:2022-05-24 02:58:38

假设您在Windows窗体中,您需要在 DataGridView c中添加一个 DataGridViewButtonColumn code> - 不直接到 DataTable

Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn to your DataGridView - Not directly to the DataTable.

这应该发生在绑定 DataTable DataGridView

这样的东西应该有效:

Something like this should work:

DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "uninstall_column";
uninstallButtonColumn.Text = "Uninstall";
int columnIndex = 2;
if (dataGridViewSoftware.Columns["uninstall_column"] == null)
{
    dataGridViewSoftware.Columns.Insert(columnIndex, uninstallButtonColumn);
}

当然,你必须处理 CellClick 事件的网格做任何事情的按钮。

Of course you will have to handle the CellClick event of the grid to do anything with the button.

将其添加到您的DataGridView初始化代码中的某个位置

Add this somewhere in your DataGridView Initialization code

dataGridViewSoftware.CellClick += dataGridViewSoftware_CellClick;

然后创建处理程序:

private void dataGridViewSoftware_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridViewSoftware.Columns["uninstall_column"].Index)
    {
        //Do something with your button.
    }
}