DataKeyNames coming up as undesired Update parameters in ASP.NET GridView
I have defined the following in my gridview markup (simplified):
<asp:GridView ID="grvReport" runat="server" DataSourceID="odsReport"
AutoGenerateColumns="False" DataKeyNames="EntryDate,EmployeeNumber">
Then I define my datasource's update parameters as such:
<UpdateParameters> 开发者_如何学Go
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="IsJustified" Type="Boolean" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="LastEditor" Type="String" />
</UpdateParameters>
But when trying to update I get an error saying
could not find a non-generic method 'Update' that has parameters: ID, IsJustified, Comment, LastEditor, EntryDate, EmployeeNumber.
Can I avoid having the fields under DataKey coming up as parameters for my methods?
NOTE: I can work around this by simply modifying my method's parameters and adding the extra values coming from the Gridview and doing nothing with them but it would be nice to have a way to avoid these values even being sent at all.
Thanks in advance,
EtonB.You can remove them using the ObjectDataSource Updating Event
void ods_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
e.InputParameters.Remove("Type_ID");
e.InputParameters.Remove("Document_ID");
e.InputParameters.Remove("State_ID");
}
I am using the DataKeys for lookup purposes but I don't need them in my update procedure.
I think there is no workaround. Microsoft recognized this issue and says it will be solved in a next version. Check here for more info: https://connect.microsoft.com/VisualStudio/feedback/details/260674/generated-default-update-method-in-datatableadapter-in-a-typed-dataset-shouuld-not-update-primary-key.
Data key names represent the primary key to the data you're updating, so I wouldn't think you could/should avoid having them in the update method. If the ID is is truly the primary key, then it should be listed under your data key names instead. Else your update method should take the EntryDate and EmployeeNumber as a parameter instead of ID.
精彩评论