在有条件的基础上绑定gridview

时间:2013-06-04 07:10:08

标签: sql gridview stored-procedures data-binding

其实我有一个Gridview。我需要在有条件的基础上绑定它。见下面的代码

<asp:GridView ID="GVReport" runat="server" AutoGenerateColumns="False" 
     EnableModelValidation="True" Width="770px" 
     DataKeyNames="User_Id" 
     CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" 
     onpageindexchanging="GVReport_PageIndexChanging" 
     onrowdatabound="GVReport_RowDataBound" ShowFooter="True" PageSize="31">
     <AlternatingRowStyle BackColor="White" />
     <Columns>
         <asp:TemplateField HeaderText="Date">
             <ItemTemplate>
                 <asp:Label ID="Label1" runat="server" 
                      Text='<%# Convert.ToDateTime(Eval("Login_Date")).ToString("dd/MM/yyyy") %>'></asp:Label>
             </ItemTemplate>
             <HeaderStyle HorizontalAlign="Left" />
             <ItemStyle HorizontalAlign="Left" />
         </asp:TemplateField>

         <asp:TemplateField HeaderText="In Time">
             <ItemTemplate>
                 <asp:Label ID="Label2" runat="server" 
                      Text='<%# String.Format("{0:t}", Eval("In_Time")) %>'></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>

         <asp:TemplateField HeaderText="Out Time">
             <ItemTemplate>
                 <asp:Label ID="Label3" runat="server" Text='<%#String.Format("{0:t}", Eval("Out_Time")) %>'></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>

         <asp:BoundField HeaderText="HRs" DataField="Hrs">
             <FooterStyle HorizontalAlign="Left" />
             <HeaderStyle HorizontalAlign="Left" />
             <ItemStyle HorizontalAlign="Left" />
         </asp:BoundField>

         <asp:BoundField DataField="Comments" HeaderText="Late Reason" />
         <asp:BoundField DataField="Out_Reason" HeaderText="Out Reason" />
         <asp:BoundField DataField="Attendance_Status" HeaderText="Status" />
     </Columns>
     <EditRowStyle BackColor="#2461BF" />
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" 
                  HorizontalAlign="Left" />
     <PagerStyle BackColor="#2461BF" ForeColor="White" 
                 HorizontalAlign="Center" />
     <RowStyle BackColor="#EFF3FB" />
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 </asp:GridView>

和我的存储过程如下

select 
    User_Id, Login_Date, Week_Day, 
    In_Time, Out_Time, Late_Reason, Out_Reason,     
    Attendance_Status 
from 
    mtblAttendance 
where  
    Login_Date between @Date1 and @Date2 
    and User_Id = @User_Id 
order by 
    Login_Date 

我的条件是,当Out_Reason列具有空值或空白时,Out_Time不应该绑定而是另一列应该绑定在那里。

让我们说

if(Out_Reason=null)
{
     // then bind the column3 instead of Out_Time
}

0 个答案:

没有答案