My question is two-fold: firstly, is it a mistake by Microsoft that when the autogeneratecolumns=true attribute of an asp.net gridview is set, columns are generated even for fields listed in the datakeynames attribute? Surely that is contrary to the whole rationale for datakeys? Secondly, is there a way to use datakeys without having to explicitly list all the non-datakey columns in the grid as bound fields, which is tedious and bad for maintenance?
In the light of AVD's comment, I will give an example: suppose I have this query as the source for my grid
SELECT intAssessmentElementID, strModuleCode, strAssessmentElement,
Weighting, intSemester, SubmissionDate FROM tblAssessmentElements
and I don't want to display intAssessmentElementID. Here is my gridview definition
<asp:GridView ID="grdAssessmentElements" runat="server" DataKeyNames="intAssessmentElementID">
<Columns>
<asp:BoundField HeaderText="Module Code" DataField="strModuleCode" />
<asp:BoundField HeaderText="Element" DataField="strAssessmentElement" />
<asp:BoundField HeaderText="Weighting" DataField="Weighting" />
<asp:BoundField HeaderText="Semester" DataField="intSemester" />
<asp:BoundField HeaderText="Submission Date" DataField="SubmissionDate" />
</Columns>
</asp:GridView>
Writing out those bound fields is tedious, and if I add a field to the query, I have to remember to add it to the grid definition too. With autogeneratecolumns=true, the update would be automatic. But autogeneratecolumns=true generates all columns, even those listed in datakeynames. I think this is contralogical. I would like a way to use both autogeneratecolumns=true and datakeynames, but with the datakeynames columns not appearing. I have seen numerous suggestions to hide the columns after data bind, but they don't work for me and others When the columns are auto-generated, they don't appear to be available/generated even in PreRender.