所以我有一个gridview:
<asp:GridView ID="grv_existingEmployee" runat="server" AutoGenerateColumns="False"
OnRowCommand="grv_existingEmployee_RowCommand"
onrowcancelingedit="grv_existingEmployee_RowCancelingEdit"
onrowdeleted="grv_existingEmployee_RowDeleted"
onrowdeleting="grv_existingEmployee_RowDeleting"
onrowediting="grv_existingEmployee_RowEditing">
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="but_delete" runat="server" Text="Delete" CommandName="Delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
<asp:Button ID="but_edit" runat="server" Text="Edit" CommandName="Edit" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="but_cancel" runat="server" Text="Cancel" CommandName="Cancel" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</EditItemTemplate>
</asp:TemplateField>
</asp:GridView>
我试图这样做,当用户点击按钮时,我更改selectedIndex,这是我的代码:
protected void grv_existingEmployee_RowCommand(object sender, GridViewCommandEventArgs e) {
string currentCommand = e.CommandName;
int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
if (currentCommand == "Edit") {
grv_existingEmployee.SelectedIndex = currentRowIndex;
}
if (currentCommand == "Delete") {
grv_existingEmployee.SelectedIndex = -1;
}
if (currentCommand == "Cancel") {
grv_existingEmployee.SelectedIndex = -1;
}
grv_existingEmployee.DataSource = myDataSource
grv_existingEmployee.DataBind();
}
不幸的是,这不会设置所选的索引,任何想法?
编辑:在进一步测试时,他们会更改所选索引,但仅限于第二次回发。
答案 0 :(得分:0)
事实证明
grv_existingEmployee.SelectedIndex = currentRowIndex;
还不够,修复它的原因还在于:
grv_existingEmployee.EditIndex = currentRowIndex;