GridView控件使用技巧——用DataKeyNames来保存绑定的datatable更多的字段值
1 GridView控件代码
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="1" ForeColor="#333333" OnRowDataBound="GridView1_RowDataBound"
Width="100%" DataKeyNames="FNO,FID,FDetail" meta:resourcekey="GridView1Resource1">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle CssClass="tabletitle" />
<Columns>
<asp:BoundField DataField="Fclass" HeaderText="班级" ReadOnly="True" meta:resourcekey="BoundFieldResource1">
<ItemStyle HorizontalAlign="Left" Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="FName" HeaderText="名字" ReadOnly="True" meta:resourcekey="BoundFieldResource2">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="FDetail" HeaderText="操作" ReadOnly="True" meta:resourcekey="BoundFieldResource3">
<ItemStyle HorizontalAlign="Left" Width="150px" />
</asp:BoundField>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<HeaderTemplate>
<asp:Label ID="lbAuthorize" runat="server" Text="选择" meta:resourcekey="Resource1"></asp:Label>
<asp:CheckBox ID="chkSelectAll" runat="server" onclick="selectAll(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Text=" " />
</ItemTemplate>
<ItemStyle Width="120px" />
<HeaderStyle Width="120px" />
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Left" />
</asp:GridView>
2DataKeyNames使用
调用DataKeyNames="FNO,FID,FDetail" 字段的值如下
GridView1.DataKeys[row.RowIndex].Values[0]
GridView1.DataKeys[row.RowIndex].Values[1]
GridView1.DataKeys[row.RowIndex].Values[2]