2014-05-21 156 views
0

我綁定複選框字段數據庫,並希望顯示實際值,如果在數據庫中它是1,那麼複選框應在gridview中打勾,否則未選中。我試過了,但是我的代碼沒有顯示覆選框中的實際值,總是沒有選中。綁定複選框與數據庫列

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 
+0

你能在那裏你也請張貼代碼中的數據綁定,在後面的類代碼。謝謝 – Christos

回答

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值也,那麼你需要做的checkingunchecking從C#代碼RowDataBound事件。

0

試試這個

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

而是採用

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