从网格视图中获取业务对象

发布于 2024-09-06 16:53:35 字数 1714 浏览 4 评论 0原文

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

这是我的 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>

我绑定它与我的业务对象列表< Patient >.. 在行 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 为空...为什么会这样...我错过了什么..

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

What exactly is the e.Row.DataItem return.. MSDN says.. returns An Object that represents the underlying data object to which the GridViewRow object is bound.

Here is my 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>

And I bind it with my Business Object List < Patient >.. In the row DataBound event, I try this...

    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 = "";
        }
    }

I get the exception P1 is null... Why so... What am I missing..

Note : There could be other ways to accomplish this, but I'm looking especially why my p1 is null... and is there any way to get the Patient Object back from GridView after binding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-09-13 16:53:35

页眉和页脚也会调用 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;

RowDataBound is called for the header and footer also, you need to make sure your currently in an actual DataRow before calling e.Row.DataItem:

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

       Patient p1 = (Patient)e.Row.DataItem;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文