我想在gridview rowUpdating事件中获取单元格的新值:
roles.RoleName = gvRoles.Rows[e.RowIndex].Cells[GetColumnIndexByName(row, "RoleName")].Text;
但该值为空。
我检查了 e.NewValues,它是一个有序字典。它包含新的更改值。如何检索新值进行更新?
aspx 设计是:
<asp:GridView ID="gvRoles" DataKeyNames="RoleId" runat="server"
AutoGenerateColumns="False" GridLines="Vertical" CssClass="table
table-striped table-bordered" AutoGenerateEditButton="True"
OnRowCancelingEdit="gvRoles_RowCancelingEdit" OnRowUpdating="gvRoles_RowUpdating"
OnRowEditing="gvRoles_RowEditing">
<Columns>
<asp:BoundField DataField="RoleId" HeaderText="RoleId" ReadOnly="True" />
<asp:BoundField DataField="RoleName" HeaderText="RoleName" ReadOnly="false" />
<asp:BoundField DataField="Role_Description" HeaderText="Role Description" ReadOnly="false" />
<asp:TemplateField HeaderText="RoleStatus">
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" SelectedValue='<%# Bind("Role_Status") %>' >
<asp:ListItem>True</asp:ListItem>
<asp:ListItem>False</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRoleStatus" runat="server"
Text='<%# Bind("Role_Status") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
最佳答案
我发现的最简单的方法是发现e.NewValues
。正如我上面提到的,它是一个有序的字典,只有我需要管理它。
我需要从这个字典中检索新值,这是通过这种方式完成的。
if (!string.IsNullOrEmpty(e.NewValues["RoleName"].ToString()))
{
roles.RoleName = e.NewValues["RoleName"].ToString();
}
如果您有模板字段,它也适用;
if (!string.IsNullOrEmpty(e.NewValues["Role_Status"].ToString()))
{
roles.Role_Status = Convert.ToBoolean(e.NewValues["Role_Status"].ToString());
}
这是我发现的最简单的事情。在此之前,我只想使用 Find 控件和类型转换,然后检索所有批处理代码。
还有 e.OldValues 有序字典。每个人都可以用它来比较新值和旧值。如果值相同,他们可以通知用户更改值(给出新的单元格值)。
关于c# - 如何获取RowUpdating事件GridView中的单元格值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487748/