将复选框与数据库列绑定

时间:2014-05-21 07:26:04

标签: c# asp.net gridview ado.net

我将Checkbox字段绑定到数据库,并希望显示实际值,如果在数据库中它是1,那么复选框应该在gridview中勾选,否则不选中。我试过,但我的代码没有在复选框中显示avtual值,始终保持未选中状态。

CODE:

<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" runat="server" AutoGenerateColumns="false" CssClass="mGrid"  Width="450px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
               <Columns>
                <asp:BoundField HeaderText="ID" DataField="ID" /> 
                <asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />  
                <%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" />  --%>
                <asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" /> 
                <asp:BoundField HeaderText="Message" DataField="Message" />   
                <asp:TemplateField HeaderText="IsComplaint">
                 <ItemTemplate>
                  <asp:CheckBox ID="ckboxIsComplaint" OnCheckedChanged="ckboxIsComplaint_CheckedChanged" AutoPostBack="true" runat="server" Value='<%# Eval("IsComplaint") %>' />
                 </ItemTemplate>
                </asp:TemplateField>
               </Columns>
            </asp:GridView>

的.cs

SELECT [ID]
      ,REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 106), ' ','/') as RecievingDate
      ,[ToMobileNo]
      ,[FromMobileNo]
      ,[Message],
       [IsComplaint]
      FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
      where Convert(date,ReceivedDateTime)>= @DateFrom AND Convert(date,ReceivedDateTime)<= @DateTo AND IsComplaint=1

2 个答案:

答案 0 :(得分:0)

如果您只获得truefalse,则可以使用以下内容添加

Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>'CheckBox

<asp:CheckBox ID="ckboxIsComplaint" OnCheckedChanged="ckboxIsComplaint_CheckedChanged" AutoPostBack="true" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />

如果您还获得null值,则需要在checking事件中执行C#代码中的uncheckingRowDataBound

答案 1 :(得分:0)

试试这个

<asp:CheckBoxField DataField="IsComplaint" HeaderText="IsComplaint"  SortExpression="IsComplaint" />

而不是使用

<asp:TemplateField HeaderText="IsComplaint"> ............. </asp:TemplateField>