如何向可绑定的表适配器添加属性?
我有一个数据库表 TravelRequest
,其中包含字段 SignOffName
和 SignOffDate
。 我有一个基于此表的表适配器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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是在单个不可编辑字段中显示合并结果,您可以这样做:
If your objective is to display the combined result in a single non-editable field you can do it like this:
如果您不想更改表结构,请更改将数据加载到适配器中的默认方法(通常称为 Fill),很可能您正在执行 Select * From [TravelRequest] 或选择各个字段,所以您可以要做的是将 TableAdapter 查询选择语句更改为
“修改默认查询”之类的内容,然后选择 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
Modifying the default query, and selecting SignOffNameDate as null will give you access to set this value