[英]How to add a button to a column in the DataGridView
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Software Title", typeof(string)));
dt.Columns.Add(new DataColumn("Version", typeof(string)));
dt.Columns.Add(new DataColumn("Uninstall", typeof(System.Windows.Forms.Button)));
DataRow dr = dt.NewRow();
dr[0] = "App";
dr[1] = "1.0";
Button uninstall = new Button();
uninstall.Text = "Uninstall";
dr[2] = uninstall;
dt.Rows.Add(dr);
dataGridViewSoftware.DataSource = dt;
The text appears but button never shows up.文本出现,但按钮永远不会出现。
Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn
to your DataGridView
- Not directly to the DataTable
.假设您在 Windows 窗体中,您需要将
DataGridViewButtonColumn
添加到您的DataGridView
- 而不是直接添加到DataTable
。
This should occur somewhere after you bind the DataTable
to the DataGridView
.这应该在您将
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);
}
Of course you will have to handle the CellClick
event of the grid to do anything with the button.当然,您必须处理网格的
CellClick
事件才能对按钮执行任何操作。
Add this somewhere in your DataGridView Initialization code将此添加到您的 DataGridView 初始化代码中的某处
dataGridViewSoftware.CellClick += dataGridViewSoftware_CellClick;
Then create the handler:然后创建处理程序:
private void dataGridViewSoftware_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridViewSoftware.Columns["uninstall_column"].Index)
{
//Do something with your button.
}
}
Make it simple.让它变得简单。
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
{
button.Name = "button";
button.HeaderText = "Button";
button.Text = "Button";
button.UseColumnTextForButtonValue = true; //dont forget this line
this.dataGridView1.Columns.Add(button);
}
You can use the best method using below您可以使用以下最佳方法
gvEmployees.AutoGenerateColumns = false;
gvEmployees.ColumnCount = 4;
DataGridViewButtonColumn SelectButton = new DataGridViewButtonColumn();
SelectButton.Name = "Select";
SelectButton.Text = "Select";
SelectButton.UseColumnTextForButtonValue = true;
if (gvEmployees.Columns["Select"] == null)
{
gvEmployees.Columns.Insert(0, SelectButton);
}
DataGridViewButtonColumn DeleteButton = new DataGridViewButtonColumn();
DeleteButton.Name = "Delete";
DeleteButton.Text = "Delete";
DeleteButton.UseColumnTextForButtonValue = true;
if (gvEmployees.Columns["Delete"] == null)
{
gvEmployees.Columns.Insert(1, DeleteButton);
}
gvEmployees.Columns[2].Name = "EmployeeID";
gvEmployees.Columns[2].HeaderText = "EmployeeID";
gvEmployees.Columns[2].DataPropertyName = "EmployeeID";
Please let me know that how can I clear the button after added to the datagridview. 请告诉我,添加到datagridview后如何清除按钮。 Following three procedures are not working.
以下三个步骤不起作用。
datagridview1.DataSource = null;
datagridview1.Rows.Clear();
datagridview1.Refresh();
Thanks H. Paul. 谢谢保罗。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.