gpt4 book ai didi

c# - 从 GridView 中获取业务对象

转载 作者:太空狗 更新时间:2023-10-30 00:46:31 25 4
gpt4 key购买 nike

e.Row.DataItem 返回的到底是什么。MSDN 说。返回一个对象,表示 GridViewRow 对象绑定(bind)到的基础数据对象。

这是我的 DataGrid...

    <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>

然后我将它与我的业务对象 List < Patient > 绑定(bind)。在 DataBound 事件行中,我尝试这个...

    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 is null...为什么会...我错过了什么..

注意:可能还有其他方法可以实现此目的,但我特别在寻找为什么我的 p1 为空...并且有什么方法可以获取 Patient 绑定(bind)后从 GridView 返回对象。

最佳答案

RowDataBound 也为页眉和页脚调用,在调用 e.Row.DataItem 之前,您需要确保当前处于实际的 DataRow 中:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType != DataControlRowType.DataRow)
return;

Patient p1 = (Patient)e.Row.DataItem;

关于c# - 从 GridView 中获取业务对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3091265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com