在 gridview 中分页,第二页为空,gridview 在按钮单击事件上填充

paging in gridview , 2nd page empty , gridview populated on a button click event

我已经搜索过这个问题,但没有一个答案能解决我的问题。

当用户键入一个词一个词并单击一个按钮时,SqlDataAdapter 会搜索数据库并将结果放入数据table 以填充 gridview。 在gridview中启用分页时,只有gridview的第一页显示数据!

这是我的代码。这是我的数据 table 定义的地方:

private DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
}

protected void btn_Search_Click(object sender, EventArgs e)
{
    kcestring.DataSource = @"localhost";
    kcestring.InitialCatalog = "KCE";
    kcestring.UserID = "sa";
    kcestring.Password = "123";

    SqlDataAdapter searchadap = newSqlDataAdapter("sp_GetDevicePropByDeviceName2", kcestring.ToString());
    searchadap.SelectCommand.CommandType = CommandType.StoredProcedure;

    SqlParameter categoryID = new SqlParameter("categoryID", SqlDbType.BigInt);

    categoryID.Value = drp_SubCategories.SelectedValue;

    searchadap.SelectCommand.Parameters.Add(categoryID);

    DataTable dt = new DataTable();
    searchadap.Fill(dt);

    grv_Device.DataSource = dt;
    grv_Device.DataBind();
}

protected void grv_Device_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grv_Device.DataSource = dt;
    grv_Device.PageIndex = e.NewPageIndex;

    grv_Device.DataBind();
}

只需删除

DataTable dt = new DataTable();

之前

searchadap.Fill(dt);

原因是您定义了两个 dt(一个全局的,另一个局部于事件 btn_Search_Click)。在 btn_Search_Click 事件中,您正在填充本地 dtgrv_Device_PageIndexChanging 事件未获取任何行,因为它正在访问全局变量。两者是不同的变量。

答案太大了,无法发表评论! 这是我的页面加载,它包含程序其他部分使用的内容:

protected void Page_Load(object sender, EventArgs e)
    {

        btn_Edit.Enabled=false ;
        lbl_error.Visible = false;
        if (!Convert.ToBoolean(Session["logedin"]))
        {
            Response.Redirect("Default.aspx");
        }
        hiddenitems.Visible = false;
        if (Page.IsPostBack)
        {   btn_Search.Visible = true;
            lbtn_advacedsearch.Visible = true;

            drp_Property.Visible = true;
            txt_pvalue.Visible = true;
            Label5.Visible = true;
            Label4.Visible = true;


        }

        img_Logo.Visible = false;
        //imgLogo.Src = "pics/Manufacturer_Logo/selectmodel.jpg";
        SqlConnectionStringBuilder kcestring = new SqlConnectionStringBuilder();
        kcestring.DataSource = @"localhost";
        kcestring.InitialCatalog = "KCE";
        kcestring.UserID="sa";
        kcestring.Password="123";
        //kcestring.IntegratedSecurity = true;
        SqlDataAdapter empper = new SqlDataAdapter("sp_GetEmployeepermissionsByID", kcestring.ToString());
        SqlParameter employeeID = new SqlParameter("employeeID", SqlDbType.BigInt);
        employeeID.Value = Session["employeeid"];

        empper.SelectCommand.Parameters.Add(employeeID);
        empper.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataTable da = new DataTable();
        empper.Fill(da);

    }

PageIndexChanging 中使用此代码:

{
    GridView1.PageIndex = e.NewPageIndex;
    SqlCommand cmd = new SqlCommand("Select * from Requseted_movie ORDER BY [ID] DESC", con);

    SqlDataAdapter DA1 = new SqlDataAdapter(cmd);
    DA1.Fill(DT1);

    GridView1.DataSource = DT1;
    GridView1.DataBind();
}