首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不绑定的情况下从数据源对网格视图进行分页

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

Stack Overflow用户
提问于 2017-09-08 06:50:41
回答 2查看 42关注 0票数 0
代码语言:javascript
复制
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();
            }

    }
}    

如何在一些站点中进行分页,它们已经给出了绑定网格,但是如果我在这里使用了绑定网格,我就不能在submit按钮上运行它了?

代码语言:javascript
复制
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");
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-12 06:17:45

代码语言:javascript
复制
  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();

            }
票数 0
EN

Stack Overflow用户

发布于 2017-09-08 06:56:16

GridView控件有一个分页选项,如下所述:https://msdn.microsoft.com/en-us/library/aa479347.aspx

代码语言:javascript
复制
<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。

您还可以以编程方式指定属性。

代码语言:javascript
复制
GridView1.AllowPaging=true;
GridView1.PageSize=50;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46110293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档