如何在下拉列表更改事件中创建绑定到数据集的分页网格视图?

时间:2015-04-14 10:47:05

标签: c# binding webforms pagination dataset

我使用PageLoad上的数据集和数据绑定来自sqlserver的gridview。

public DataSet Ds
{
    get
    {
        object temp = ViewState["Ds"];
        return temp == null ? null : temp as DataSet;
    }
    set
    {
        ViewState["Ds"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridCustAllInfoBind();

    }
}

public void gridCustAllInfoBind()
{
    try
    {
        //open the db connection if it is closed...  
        if (connection.State == ConnectionState.Closed)
            connection.Open();
        command = new SqlCommand();
        command.CommandText = "Z_cust";
        command.CommandType = CommandType.StoredProcedure;
        command.Connection = connection;           

        SqlDataAdapter daAcc = new SqlDataAdapter(command);

        this.Ds = new DataSet();
        daAcc.Fill(Ds);


        if (Ds.Tables[0].Rows.Count == 0)
        {
            Ds.Tables[0].Rows.Add(Ds.Tables[0].NewRow());
            gridCustomer.DataSource = Ds;
            gridCustomer.DataBind();
            int columncount = gridCustomer.Rows[0].Cells.Count;
            gridCustomer.Rows[0].Cells.Clear();
            gridCustomer.Rows[0].Cells.Add(new TableCell());
            gridCustomer.Rows[0].Cells[0].ColumnSpan = columncount;
            gridCustomer.Rows[0].Cells[0].Text = "No Records Found";
        }
        else
        {
            //gridCustomer.DataSource = idr;
            gridCustomer.DataSource = Ds;
            gridCustomer.DataBind();
        }           
    }
    catch (Exception ex)
    {
        lblMessagePaySerach.Text= ex.Message;
    }  
    finally //Close db Connection if it is open....  
    {
        if (connection.State == ConnectionState.Open)
            connection.Close();

    }
}

这里我按需过滤数据集,然后将gridview绑定到它。

protected void btnSearchCust_Click(object sender, EventArgs e)
{
    if (ddlStatus.SelectedIndex == 0 && ddlColumns.SelectedIndex == 0)
    {
        var strExpr = "Status='Active'";            

        var dv = Ds.Tables[0].DefaultView;
        dv.RowFilter = strExpr;
        var newDS = new DataSet();
        var newDT = dv.ToTable();
        newDS.Tables.Add(newDT);

        gridCustomer.DataSource = newDS;
        int size = int.Parse(ddlPaging.SelectedItem.Value.ToString());
        gridCustomer.PageSize = size;
        gridCustomer.DataBind();
    }
}

这里我选择下拉列表值,然后我必须按需显示gridview记录。

protected void ddlPaging_SelectedIndexChanged(object sender, EventArgs e)
{
    int size = int.Parse(ddlPaging.SelectedItem.Value.ToString());
    gridCustomer.PageSize = size;
    btnSearchCust_Click(sender, e);
}

0 个答案:

没有答案