0

我将 DataGridView 的 DataSource 设置为 DataTable。然后,我向 DataGridView 添加了 2 个按钮列。DataGridView 在表单上看起来很好,但是,当我在索引 1 和 6 处插入按钮时,DataGridView 将其单击事件返回为索引 0 和 1。这似乎是通过在 DataSource 之外添加按钮来实现的。

有关如何解决此问题的任何建议?

DataTable dataTable= someMethod();

DataGridView.DataSource = dataTable;

// Add a button to the DataGridView at the specified position
DataGridViewButtonColumn btnEdit = new DataGridViewButtonColumn();
btnEdit.Name = "btnEdit";
btnEdit.Text = "Edit";
btnEdit.HeaderText = "Edit Quantity";
btnEdit.UseColumnTextForButtonValue = true;
DataGridView.Columns.Insert(1, btnEdit);

DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn();
btnDelete.Name = "btnDelete";
btnDelete.Text = "Delete";
btnDelete.HeaderText = "Delete";
btnDelete.UseColumnTextForButtonValue = true;
DataGridView.Columns.Insert(6, btnDelete);
4

0 回答 0