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

时间:2015-01-19 14:43:46

标签: c# asp.net gridview

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

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

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

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();
}

3 个答案:

答案 0 :(得分:1)

只需删除

即可
DataTable dt = new DataTable();

<击>

searchadap.Fill(dt);

原因是您已定义了两个dt(事件btn_Search_Click的一个全局和另一个本地)。在btn_Search_Click事件中,您填写的是本地dtgrv_Device_PageIndexChanging事件没有获取任何行,因为它访问全局变量。两者都是不同的变量。

答案 1 :(得分:0)

答案是大评论! 这是我的页面加载它包含在程序的其他部分使用的东西:

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);

    }

答案 2 :(得分:0)

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();
}