我有一个像这样的GridView:

<asp:GridView ID="GridViewAllPeopleEditMode" runat="server"
    AutoGenerateColumns="false"
    AutoGenerateEditButton="true"
    AllowPaging="true"
    PageSize="20"
    OnRowEditing="GridViewAllPeopleEditMode_RowEditing"
    OnRowCancelingEdit="GridViewAllPeopleEditMode_RowCancelingEdit"
    OnRowUpdating="GridViewAllPeopleEditMode_RowUpdating"
    OnPageIndexChanging="GridViewAllPeopleEditMode_PageIndexChanging">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="BusinessEntityID" ReadOnly="true"/>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName"/>
        <asp:BoundField DataField="MiddleName" HeaderText="MiddleName"/>
        <asp:BoundField DataField="LastName" HeaderText="LastName"/>
    </Columns>
</asp:GridView>


我希望page_load在设置DataSource()/ DataBind()之前先设置DataField属性,而不是自己在.aspx页中编写它们。

是否可以这样做,还是必须更改BoundFields?
非常感谢!

最佳答案

您可以在代码端这样使用

C#
BoundField field = (BoundField)this.GridViewAllPeopleEditMode.Columns[0];
field.DataField = "To";

VB
Dim field As BoundField = DirectCast(Me.GridViewAllPeopleEditMode.Columns(0), BoundField)
field.DataField = "To"

10-08 11:34