【发布时间】:2023-04-07 23:19:02
【问题描述】:
我有一个没有数据键的网格视图。当我单击行编辑按钮时,我被带到了行编辑方法。我尝试了几种方法来获取行索引,但没有运气。
protected void gvwCustomerLocation_RowEditing(object sender, GridViewEditEventArgs e)
{
var index1 = e.NewEditIndex; //gives me an incorrect index number
//The SelectedIndex always equals -1
for (int i = 0; i < gvwCustomerLocation.Rows.Count; i++)
{
if (gvwCustomerLocation.SelectedIndex == 1)
{
}
}
int index = gvwCustomerLocation.EditIndex; //The EditIndex = -1
GridViewRow row = gvwCustomerLocation.SelectedRow; //The SelectedRow = null
var test = row.RowIndex.ToString();
}
如何获取选中的行索引?
【问题讨论】:
-
您应该可以使用e.NewEditIndex 和
gvwCustomerLocation.Rows[e.NewEditIndex]来获取选定的行。 -
@DaveAnderson 不幸的是,它没有。当我单击 gridview 中的第一行时,index1 的值是 2 而不是 0。
-
你的GridView数据绑定是什么,当你使用
gvwCustomerLocation.Rows[e.NewEditIndex]时,行中有什么数据? -
@DaveAnderson 它绑定到一个 DataSet 表。
-
你的数据集有多少行?如果
e.NewEditIndex = 2是你数据集中的哪一行?您是如何连接事件处理程序的?