繁体   English   中英

如何在不进行数据绑定的情况下从数据源提供网格视图的分页

[英]How to give paging for grid view from data source without databinding

public partial class _Default : System.Web.UI.Page
{
    protected void submit_Click(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection();

            con.ConnectionString = "Data source= LAPTOP-6\\SQLEXPRESS;Initial Catalog=training;integrated Security=true";
            SqlCommand cmd = new SqlCommand();
             cmd.CommandText = "Sp_PO";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@date1", TextBox1.Text.ToString());
            cmd.Parameters.AddWithValue("@date2", TextBox2.Text.ToString());
            cmd.Connection = con;

            try
            {
                con.Open();

                GridView1.EmptyDataText = "No Records Found";

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                GridView1.DataSource = ds;

                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                lblerror.Text = ex.Message.ToString();

            }
            finally
            {
                con.Close();
                con.Dispose();
            }

    }
}    

如何在某些站点中提供分页,他们已经给出了绑定网格,但是如果我在这里使用绑定网格,则无法在“提交”按钮上运行它?

protected void reset_Click(object sender, EventArgs e)
{
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
}

protected void save_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data source= LAPTOP-6\\SQLEXPRESS;Initial Catalog=training;integrated Security=true";
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        CheckBox check = (CheckBox)gvr.FindControl("check");
        if (check.Checked)
        {
            conn.Open();
            string data1 = ((Label)gvr.FindControl("pomas_pono")).Text;    
            string data2 = ((Label)gvr.FindControl("pomas_suppliercode")).Text;
            string data3 = ((Label)gvr.FindControl("pomas_pobasicvalue")).Text;
            string data4 = ((Label)gvr.FindControl("poitm_itemcode")).Text;
            string data5 = ((Label)gvr.FindControl("poitm_order_quantity")).Text;
            string data6 = ((Label)gvr.FindControl("poitm_itemvalue")).Text;
            string data7 = ((Label)gvr.FindControl("poitm_itemdescription")).Text;
            string data8 = ((TextBox)gvr.FindControl("remarks")).Text;

            SqlCommand command = new SqlCommand("INSERT INTO po_remarks" + "(pomas_pono,pomas_suppliercode,pomas_pobasicvalue,poitm_itemcode,poitm_order_quantity,poitm_itemvalue,poitm_itemdescription,remarks) VALUES(@pomas_pono,@pomas_suppliercode,@pomas_pobasicvalue,@poitm_itemcode,@poitm_order_quantity,@poitm_itemvalue,@poitm_itemdescription,@remarks)", conn);   //generate insert sql using above data
            command.Parameters.AddWithValue("@pomas_pono", data1);
            command.Parameters.AddWithValue("@pomas_suppliercode", data2);
            command.Parameters.AddWithValue("@pomas_pobasicvalue", data3);
            command.Parameters.AddWithValue("@poitm_itemcode", data4);
            command.Parameters.AddWithValue("@poitm_order_quantity", data5);
            command.Parameters.AddWithValue("@poitm_itemvalue", data6);
            command.Parameters.AddWithValue("@poitm_itemdescription", data7);
            command.Parameters.AddWithValue("@remarks", data8);
            command.ExecuteNonQuery();

        }
    }

    conn.Close();
}

protected void cancel_Click(object sender, EventArgs e)
{
    Page.Response.Redirect("Default.aspx");
}

GridView控件具有分页选项,如下所述: https : //msdn.microsoft.com/zh-cn/library/aa479347.aspx

<asp:GridView ID=" productsGridView" Runat="server" 
         DataSourceID="productDataSource" AutoGenerateColumns="False"
            AllowSorting="True" BorderWidth="2px" BackColor="White" 
            GridLines="None" CellPadding="3"
            CellSpacing="1" BorderStyle="Ridge" BorderColor="White"
            AllowPaging="True" PageSize=50>

注意以下属性:AllowPaging。 您可以使用PageSize属性指定每页的记录数。 我认为默认的PageSize是:10。

您还可以通过编程方式指定属性,例如

GridView1.AllowPaging=true;
GridView1.PageSize=50;
  protected void submit_Click(object sender, EventArgs e)
{

    BindGrid();


}

protected void BindGrid()


    {
        SqlConnection con = new SqlConnection();

        con.ConnectionString = "Data source= LAPTOP-6\\SQLEXPRESS;Initial Catalog=training;integrated Security=true";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Sp_PO";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;

        try
        {
            con.Open();

            GridView1.EmptyDataText = "No Records Found";

             DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();

            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM