Reputation: 1117
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!
Upvotes: 0
Views: 409
Reputation: 52241
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"
Upvotes: 1
Reputation: 10243
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
Upvotes: 1