DataGridView按钮单击返回错误的索引

简单的问题。 我有一个使用DataTable设置的dataGridView,然后在DataTable for buttons之后添加了2列。 当我点击一个索引为6的按钮时,它会给我索引1这是我的第一个按钮。

DataTable cartResults = cart.RunCartQuery(sb.ToString()); dgCart.DataSource = cartResults; // 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; dgCart.Columns.Insert((int)Buttons.Edit, btnEdit); DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn(); btnDelete.Name = "btnDelete"; btnDelete.Text = "Delete"; btnDelete.HeaderText = "Delete"; btnDelete.UseColumnTextForButtonValue = true; //dgCart.Columns.Insert((int)Buttons.Delete, btnDelete); dgCart.Columns.Add(btnDelete); 

我不知道为什么返回的索引是一个(这是编辑按钮)而不是我的删除按钮,它是索引6.任何想法?


问题的根源是DataGridView.DataSource是一个DataTable,然后在设置数据源后添加2个按钮列。 即使我在索引1和6处添加了按钮,DatagridView也认为它们位于索引0和1处。尽管Grid在表单上看起来很好。