用CheckBox绑定ListView中的位 [英] Binding a bit in a ListView with a CheckBox

查看:67
本文介绍了用CheckBox绑定ListView中的位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为[PortfolioPerformance]的数据库表

So I have a database table called [PortfolioPerformance]

Portfolio, int, not null
AnnualReturn, decimal(18,2), not null
Closed, bit, not null


和一个SqlDataSource


and a SqlDataSource

<asp:SqlDataSource ID="PerformanceDataSource" runat="server"

SelectCommand="SELECT [Portfolio], [AnnualReturn], [Closed] FROM [PortfolioPerformance]"/>


和一个ListView


and a ListView

<asp:ListView ID="PerformanceListView" runat="server" DataSourceID="PerformanceDataSource">
...
<ItemTemplate>
  <tr>
    <td>
      <asp:CheckBox ID="ClosedCheckBox" runat="server" Checked='<%# Eval("Closed") %>' Enabled="false" />
    </td>
  </tr>
</ItemTemplate>


在运行时,我得到一个HttpException"DataBinding:" System.Data.DataRowView不包含名称为" Closed"的属性.如果我将CheckBox更改为Checked =''false'',那么一切都很好,除了显示错误之外.

我使用了ListView,以便用户可以编辑条目,并且编辑工作正常(即SqlDataSource在插入,更新或删除语句方面没有问题,并且在ListView模板中使用Bind("Closed")可以做到这一点''抛出异常).我已经在SQL Management Studio中运行了select语句,它返回了正确的结果.任何人都可以帮忙吗?


And at runtime I get a HttpException "DataBinding: ''System.Data.DataRowView'' does not contain a property with the name ''Closed''". If I changed the CheckBox to Checked=''false'', everything is fine, except the display is wrong.

I used a ListView so that users could edit the entry, and editing works fine (that is, the SqlDataSource has no problems with the insert, update, or delete statements, and using Bind("Closed") in the ListView templates doesn''t throw exceptions). I''ve run the select statement in SQL Management Studio and it returns the correct results. Can anyone help?

推荐答案

您需要将该位转换为布尔值,请参考以下链接并将该值转换为布尔值,然后将该值分配给checked属性.


http://stackoverflow.com/questions/1561382/asp-net- binding-integer-to-checkboxs-checked-field [ ^ ]

http://forums.asp.net/t/917702.aspx/1 [ ^ ]


http://stackoverflow.com/questions/2767352/c-sharp-convert-bit-到布尔值 [ ^ ]
You need to conver the bit to boolean please refer the following links and convert the value to boolean and then assign the value to checked property..


http://stackoverflow.com/questions/1561382/asp-net-binding-integer-to-checkboxs-checked-field[^]

http://forums.asp.net/t/917702.aspx/1[^]


http://stackoverflow.com/questions/2767352/c-sharp-convert-bit-to-boolean[^]


使用三元运算符或案例或条件,您可以根据自己的条件显示结果.
By Using Ternary Operator or Case or Condition you can display the result according to your condition.


案例条件没有不行当我将select语句更改为read
A Case condition doesn''t work. When I change the select statement to read
SELECT [Portfolio]
      ,[AnnualReturn]
      ,case
         when [Closed] = 0 then 'false'
         else 'true'
       end as 'Closed'
FROM [PortfolioPerformance]


我仍然有例外.

我不明白的是,为什么视图似乎认为没有Closed的字段-''''System.Data.DataRowView''不包含名称为"Closed''的属性.我认为转换错误会产生不同的异常和错误消息.

同样,位转换仍需要显示该字段,因此,如果DataRowView看不到它,则位是否转换都无关紧要.


I still get the exception.

What I don''t understand is why the view seems to think there isn''t a field for Closed - "''System.Data.DataRowView'' does not contain a property with the name ''Closed''". I would think a conversion error would produce a different exception and error message.

Likewise, a bit conversion still needs to have the field present, so if the DataRowView can''t see it, it doesn''t matter if the bit is converted or not.


这篇关于用CheckBox绑定ListView中的位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆