Imar , today I do another little experiment to try to find out what DataKeys is.
And I end up with even more confusion. My experiment is based on the previous one.
Just modify the DataKeyNames:
Code:
<asp:GridView ID="gvwTest" runat="server" OnRowDataBound ="gvwTest_RowDataBound" OnRowCreated ="gvwTest_RowCreated"
AutoGenerateColumns="False" DataKeyNames="ArticleID,AddedBy,Title" DataSourceID="SqlDataSource1" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True" OnRowDeleted="gvwTest_RowDeleted" OnRowDeleting="gvwTest_RowDeleting" OnRowEditing="gvwTest_RowEditing" OnRowUpdated="gvwTest_RowUpdated" OnRowUpdating="gvwTest_RowUpdating" OnSelectedIndexChanged="gvwTest_SelectedIndexChanged" >
<Columns >
<asp:BoundField DataField="ArticleID" HeaderText="ArticleID" InsertVisible="False"
ReadOnly="True" SortExpression="ArticleID" />
<asp:BoundField DataField="AddedBy" HeaderText="AddedBy" SortExpression="AddedBy" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ArticleID], [AddedBy], [Title FROM [tbh_Articles]"
DeleteCommand="DELETE FROM [tbh_Articles] WHERE [ArticleID] = @ArticleID"
InsertCommand="INSERT INTO [tbh_Articles] ([AddedBy], [Title) VALUES (@AddedBy, @Title)"
UpdateCommand="UPDATE [tbh_Articles] SET [AddedBy] = @AddedBy, [Title] = @Title WHERE [ArticleID] = @ArticleID" OnUpdated="SqlDataSource1_Updated" OnUpdating="SqlDataSource1_Updating">
<DeleteParameters>
<asp:Parameter Name="ArticleID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="AddedBy" Type="String" />
<asp:Parameter Name="Title" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="AddedBy" Type="String" />
<asp:Parameter Name="Title" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
if I do this change, I found that I can not Edit the record anyway.It will successful sumit,but the result is unchanged.
if I changed the DataKeyNames to DataKeyNames="ArticleID,AddedBy" ,then the Title is Updatable while the AddedBy is not! The same thing happen if I change it to DataKeyNames="ArticleID,Title" .This time the AddedBy is Updatable while the Title is not.
if I cross out the PrimerKey of the Article Table,that it the ArticleID,then it will give me a tuntime error for both Update and Delete operation.
I watch the GridViewUpdateEventArgs e in the gvwTest_RowUpdating event handler.And I find out something regular:
The e.Keys property is always the same with the GridView's DataKeys collection.
The e.NewValues property is all the retrieve field except the PrimerKey ArticleID.
The e.OldValues property is all the retrieve field except the DataKeyNames.
I suppose the fields in the DataKeyNames can not be changed.I am rather confused at what in fact the DataKeys is ?
Thanks.
------------------------------------------------------------------------
We learn from the history that we do not learn from the history