从动态网格视图中删除项目
我有一个动态网格视图,其中包含从 Oracle 数据库中提取的数据。
<asp:GridView ID="gvData" OnRowDataBound="gvData_RowDataBound" OnPreRender="gvData_PreRender" OnSelectedIndexChanged="gvData_SelectedIndexChanged" OnLoad="gvData_Load" OnRowCommand="gvData_RowCommand" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataSourceID="DS" AllowSorting="True" AutoGenerateSelectButton="True" PageSize="10" ShowFooter="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="DS" runat="server">
</asp:SqlDataSource>
我正在尝试编写 DELETE/UPDATE 语句。我在从 gridview 获取值并将它们添加到参数 os SQL 语句中时遇到问题。
protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DS.DeleteCommand =
"DELETE FROM TABLE " +
"WHERE PROBLEM_DEPTH_MD = :PROBLEM_DEPTH_MD";
"AND API = :API " +
"AND BUS_AREA_ID = :BUS_AREA_ID";
DS.DeleteParameters.Add(new Parameter("PROBLEM_DEPTH_MD", TypeCode.String, ??GridViewItemValue??));
}
我不知道如何从 gridview 获取值。有什么建议吗?谢谢!
I have a dynamic gridview with data pulled from oracle database.
<asp:GridView ID="gvData" OnRowDataBound="gvData_RowDataBound" OnPreRender="gvData_PreRender" OnSelectedIndexChanged="gvData_SelectedIndexChanged" OnLoad="gvData_Load" OnRowCommand="gvData_RowCommand" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataSourceID="DS" AllowSorting="True" AutoGenerateSelectButton="True" PageSize="10" ShowFooter="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="DS" runat="server">
</asp:SqlDataSource>
I am trying to write DELETE/UPDATE statements. I am having a problem with getting values from gridview and adding them to the parameters os SQL statements.
protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DS.DeleteCommand =
"DELETE FROM TABLE " +
"WHERE PROBLEM_DEPTH_MD = :PROBLEM_DEPTH_MD";
"AND API = :API " +
"AND BUS_AREA_ID = :BUS_AREA_ID";
DS.DeleteParameters.Add(new Parameter("PROBLEM_DEPTH_MD", TypeCode.String, ??GridViewItemValue??));
}
I don't know how to get a value from the gridview. Any suggestions? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要在主键列的网格视图中提及
DataKeyName
,然后您可以访问 Gridview 的选定行的值,例如...e.Keys["DataKeyName "]
如果要访问非关键字段,那么应该像...
e.Values["FieldName"]
提及 DataKeyName 就像...
< asp:GridView ID="gvData" DataKeyNames="PrimaryKeyColumn"
First off you need to mention the
DataKeyName
to your gridview of your Primary key Column and then you can access the value of the Selected row of the Gridview like...e.Keys["DataKeyName"]
If you want to access a non key field, then it should be like...
e.Values["FieldName"]
Mention DataKeyName like...
<asp:GridView ID="gvData" DataKeyNames="PrimaryKeyColumn"
您需要为 GridView 设置 DataKeyNames。
然后,您可以使用 GridViewDeleteEventArgs 来获取 Keys 属性。这将是与您设置的 DataKeyFields 相对应的键/值对的字典。本质上,它允许您获取主键——这就是您需要删除的内容。
假设主键是 PROBLEM_DEPTH_MD、API 和 BUS_ARE_ID,您可以将 DataKeyNames 设置为“PROBLEM_DEPTH_MD、API 和 BUS_ARE_ID”。
然后,您可以使用 e.Keys 在删除事件中获取这些密钥。如果您循环它们,您应该能够获取值来添加参数。
以下是关于此的更多信息:
http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
You will need to set the DataKeyNames for you GridView.
You can then use the GridViewDeleteEventArgs to get the Keys property. This will be a dictionary of Key/Value pairs which correspond to the DataKeyFields which you set. In essence it allows you to get your primary keys-- which is what you need to delete.
assuming primary keys are PROBLEM_DEPTH_MD, API, AND BUS_ARE_ID, you would set the DataKeyNames to be "PROBLEM_DEPTH_MD,API,AND BUS_ARE_ID".
You could then grab these keys in your delete event with e.Keys. If you loop over them you should be able to get the values to add your parameters.
Here is some more information on this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx