如何向可绑定的表适配器添加属性?

发布于 2024-07-12 22:55:36 字数 1164 浏览 7 评论 0原文

我有一个数据库表 TravelRequest,其中包含字段 SignOffNameSignOffDate。 我有一个基于此表的表适配器TravelRequestTable。 我有一个DetailsView 控件,它通过ObjectDataSource 使用它。 应该都是相当标准的东西。

我想要做的是将名为 SignOffNameDate 的属性添加到表适配器,该适配器组合了两个字段并且能够在DetailsView控件中绑定到它。 我想以编程方式完成此操作,而不是在 SQL 中添加另一列,因为处理空值很棘手并且取决于其他一些业务规则。

这就是我尝试过的:

public partial class TravelRequestDS
{
    public partial class TravelRequestRow
    {
        public string SignOffNameDate
        {
            get { return CombineNameDate(SignOffName, SignOffDate); }
        }
    }
}

当我以编程方式访问该属性时,该属性工作正常,但是当我尝试在我的 aspx 页面中绑定到它时:

<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
    DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
        <Fields>
            <asp:BoundField DataField="SignOffNameDate"
                HeaderText="Sign Off" />
            ...

我收到 System.Web.HttpException 异常,“名称为“SignOffNameDate”的字段或属性是在所选数据源上找不到。”

我有什么想法可以做到这一点吗? 我需要向该属性添加某个属性吗?

I have a database table TravelRequest that contains, amongst other things, the fields SignOffName and SignOffDate. I have a table adapter TravelRequestTable based on this table. I have a DetailsView control which uses this via an ObjectDataSource. Should be all fairly standard stuff.

What I want to do is add a property called SignOffNameDate to the table adapter that combines the two fields and be able to bind to it in the DetailsView control. I want to do it programmatically rather than adding another column in the SQL because dealing with null values is tricky and depends on some other business rules.

This is what I tried:

public partial class TravelRequestDS
{
    public partial class TravelRequestRow
    {
        public string SignOffNameDate
        {
            get { return CombineNameDate(SignOffName, SignOffDate); }
        }
    }
}

This property works fine when I access it programmatically, but when I try bind to it in my aspx page:

<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
    DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
        <Fields>
            <asp:BoundField DataField="SignOffNameDate"
                HeaderText="Sign Off" />
            ...

I get an System.Web.HttpException exception, "A field or property with the name 'SignOffNameDate' was not found on the selected data source."

Any ideas how I can do this? Is there an attribute I need to add to the property?

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

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

发布评论

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

评论(2

冰之心 2024-07-19 22:55:36

如果您的目标是在单个不可编辑字段中显示合并结果,您可以这样做:

<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
    DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
        <Fields>
            <asp:TemplateField HeaderText="Sign Off" SortExpression="SignOffDate">               
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("SignOffName") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("SignOffDate") %>'></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>  
            ... 

If your objective is to display the combined result in a single non-editable field you can do it like this:

<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
    DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
        <Fields>
            <asp:TemplateField HeaderText="Sign Off" SortExpression="SignOffDate">               
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("SignOffName") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("SignOffDate") %>'></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>  
            ... 
风透绣罗衣 2024-07-19 22:55:36

如果您不想更改表结构,请更改将数据加载到适配器中的默认方法(通常称为 Fill),很可能您正在执行 Select * From [TravelRequest] 或选择各个字段,所以您可以要做的是将 TableAdapter 查询选择语句更改为

Select [YourCol1],[YourCol2], SignOffNameDate as Null From [TravelRequest]

“修改默认查询”之类的内容,然后选择 SignOffNameDate 为 null 将允许您设置此值

If you don't want to change your table structure, change the default method that loads data into your adapter (usually called Fill), most likely you are doing a Select * From [TravelRequest] or selecting the individual fields, so what you could do is change your TableAdapter query select statement to something like

Select [YourCol1],[YourCol2], SignOffNameDate as Null From [TravelRequest]

Modifying the default query, and selecting SignOffNameDate as null will give you access to set this value

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