GridView分页无法正确显示数据

时间:2019-05-30 01:40:52

标签: asp.net sql-server gridview calendar

我有一个日历,当我们单击一个日期时,它将在gridview中显示从数据库中检索到的数据。由于有大量数据,因此我为gridview分页。但是,由于分页无法正确加载数据,因此分页无法正常工作。

<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="350px" NextPrevFormat="FullMonth" OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged" Width="100%">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" VerticalAlign="Bottom" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="#86F2F6" BorderColor="Black" BorderWidth="4px" Font-Bold="True" Font-Size="12pt" ForeColor="#000000" />
            <TodayDayStyle BackColor="#CCCCCC" />
        </asp:Calendar>
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" OnRowDataBound="GridView1_RowDataBound" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#ffffff" />
            <FooterStyle BackColor="#ffffff" Font-Bold="True" ForeColor="#000000" />
            <HeaderStyle BackColor="lightblue" Font-Bold="True" ForeColor="#000000" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#000000" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />

        </asp:GridView>

代码

private void BuildSocialEventTable()
    {
        SqlConnection con = new SqlConnection
        (ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString.ToString());
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT Appointment_DateTime, CAST( WO_Duration as NUMERIC(10,0)) AS WO_Duration, Appointment_ContactID, WO_MaidName, WO_DespatchName FROM Appointments WHERE (IsDelete = 0 OR IsDelete IS NULL) ", con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        socialEvents = ds.Tables[0];
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        string where = "Appointment_DateTime >= '" + e.Day.Date.ToString("dd-MMM-yyyy") + "' AND Appointment_DateTime <'" + e.Day.Date.AddDays(1).ToString("dd-MMM-yyyy") + "'";

        DataRow[] rows = socialEvents.Select(where);

        foreach (DataRow row in rows)
        {
            e.Cell.BackColor = Color.Gray;
        }
    }

    protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {
        string where = "Appointment_DateTime >= '" + Calendar1.SelectedDate.ToString("dd-MMM-yyyy") + "' AND Appointment_DateTime <'" + Calendar1.SelectedDate.AddDays(1).ToString("dd-MMM-yyyy") + "'";
        System.Data.DataView view = socialEvents.DefaultView;
        view.RowFilter = String.Format(where);

        if (view.Count > 0)
        {
            GridView1.Visible = true;
            GridView1.DataSource = view;
            GridView1.DataBind();
        }
        else
        {
            GridView1.Visible = false;
        }
    }

    private DataTable socialEvents;

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BuildSocialEventTable(); 
    }

单击https://drive.google.com/drive/folders/1SpskVi4U5LTUJGBjjyvbWNKHD9PLb0az页面后,我已经完成了什么以及将显示什么

0 个答案:

没有答案