我有一个包含多个文件的GridView。我希望能够从开始日期到结束日期获取行。得到错误

时间:2014-10-31 18:21:07

标签: asp.net vb.net gridview

我有一个带有字段的GridView,我希望能够获得开始日期和结束日期之间的行。所以我创建了一个新的SqlDataSource并将其与新的SelectCommand绑定,如下所示。我没有收到错误,但网格消失了。我的代码有什么问题或者最好的方法是什么?

   .aspx

    <div>
    <asp:TextBox ID="txtcalendarstart" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtcalendarstop" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>  
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:AttendanceDBConnectionString %>"     
SelectCommand="SELECT * FROM [tblAbsences] ORDER BY [fldEmployeeID], [fldAbsentDate], [fldAbsentCode]">

  <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource1" 
    PageSize="100" AutoGenerateColumns="False" 
    OnRowCommand="GridView1_RowCommand" Height="139px">
    <Columns>
        <%--<asp:HyperLinkField HeaderText="Edit" NavigateUrl="FormReport2.aspx" 
            Text="Edit" />--%>
        <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
               <%-- <asp:Button ID="AddButton" runat="server" 
                CommandName="Editemployee" 
                CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                Text="EditRow"  />--%>
                <asp:HyperLink ID="HyperLink1" runat="server"  NavigateUrl='<%#  "FormReport2.aspx?id=" & CType(DataBinder.Eval(Container.DataItem, "fldEmployeeID"), String) & "&Adate=" & CType(DataBinder.Eval(Container.DataItem, "fldAbsentDate"), String)& "&Acode=" & CType(DataBinder.Eval(Container.DataItem, "fldAbsentCode"), String)& "&RuleViolationW=" & DataBinder.Eval(Container.DataItem, "fldRuleViolationWarningType")& "&RuleViolationDate=" & DataBinder.Eval(Container.DataItem, "fldRuleViolationIssueDate") & "&LOAEndDate=" & DataBinder.Eval(Container.DataItem, "fldLOAENdDate") %>'  Text="Edit" />
            </ItemTemplate> 
        </asp:TemplateField>                        
        <asp:BoundField DataField="fldEmployeeID" HeaderText="EmployeeID" 
            SortExpression="fldEmployeeID" />
        <asp:BoundField DataField="fldAbsentDate" HeaderText="AbsentDate" 
            SortExpression="fldAbsentDate" />
        <asp:BoundField DataField="fldAbsentCode" HeaderText="AbsentCode" 
            SortExpression="fldAbsentCode" />
        <asp:BoundField DataField="fldRuleViolationWarningType" 
            HeaderText="Rule Violation Warning Type" 
            SortExpression="fldRuleViolationWarningType" />
        <asp:BoundField DataField="fldRuleViolationIssueDate" 
            HeaderText="Rule Violation Issue Date" 
            SortExpression="fldRuleViolationIssueDate" />
        <asp:BoundField DataField="fldLOAEndDate" HeaderText="LOA End Date" 
            SortExpression="fldLOAEndDate" />                                                                                                                         
    </Columns>
</asp:GridView>

.VB

        Dim newdatasource = New SqlDataSource
        newdatasource.ConnectionString = "Data Source=SVR-SQLDB2;Initial Catalog=AttendanceDB;Integrated Security=True"
        newdatasource.SelectCommand = "SELECT * FROM [tblAbsences] ORDER BY [fldEmployeeID], [fldAbsentDate], [fldAbsentCode] WHERE [fldAbsentDate] BETWEEN " & "2/12/2013" & " and " & "11/12/2013" & ","
        GridView1.DataSourceID = newdatasource.ID
        GridView1.DataBind()

1 个答案:

答案 0 :(得分:2)

在Where子句之后放置Order By。

newdatasource.SelectCommand = "SELECT * FROM [tblAbsences] WHERE [fldAbsentDate] BETWEEN " & "2/12/2013" & " and " & "11/12/2013" & " ORDER BY [fldEmployeeID], [fldAbsentDate], [fldAbsentCode]