返回的e.Row.DataItem究竟是什么。。MSDN说。。 返回表示GridViewRow对象绑定到的基础数据对象的对象。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="PracticeCode" HeaderText="PracticeCode" SortExpression="PracticeCode" /> <asp:BoundField DataField="AccountNo" HeaderText="AccountNo" SortExpression="AccountNo" /> <asp:BoundField DataField="PatientName" HeaderText="PatientName" SortExpression="PatientName" /> <asp:TemplateField HeaderText="Status"> <ItemTemplate> <asp:Label ID="LblStatus" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
我把它和我的业务对象绑定在一起 列表<患者>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Patient p1 = (Patient)e.Row.DataItem; Label lbl = e.Row.FindControl("LblStatus") as Label; if (p1 == null) { throw new Exception("P1 is null"); } if (p1.OpenItems.Count > 0) { lbl.Text = "Has open Items"; } else { lbl.Text = ""; } }
我有个例外 P1为空 ... 为什么。。。我错过了什么。。
注意 绑定后从GridView返回的对象。
RowDataBound也是为页眉和页脚调用的,您需要确保您当前处于实际的 DataRow e.Row.DataItem :
DataRow
e.Row.DataItem
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType != DataControlRowType.DataRow) return; Patient p1 = (Patient)e.Row.DataItem;