如何在没有数据绑定的情况下从数据源中为网格视图分页

时间:2017-09-08 06:50:41

标签: c# asp.net

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

2 个答案:

答案 0 :(得分:0)

GridView控件有一个分页选项,如下所述:https://msdn.microsoft.com/en-us/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;

答案 1 :(得分:0)

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

            }