我正在使用ASP.NET GridView。首先,我尝试使用如下的声明性字段,但没有用。我有一个错误,说明绑定对象没有属性Id或Name(但实际上确实有它们......)
<asp:GridView runat="server" ID="GlobalAnalyticsGridView" OnRowDataBound="GlobalAnalyticsGridView_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
<asp:BoundField DataField="WebAppName" HeaderText="Web Application" />
</Columns>
</asp:GridView>
然后我在RowDataBound事件上尝试了代码。它运作得很好......
protected void GlobalAnalyticsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
AnalyticsSettings settings = e.Row.DataItem as AnalyticsSettings;
(e.Row.Cells[0]).Text = settings.Id;
(e.Row.Cells[1]).Text = settings.WebAppName;
}
}
知道为什么会这样吗? 感谢您的照明:)