Dawood Abbas

Dawood Abbas

  • NA
  • 264
  • 98.2k

make paging gridview that is bound to dataset on dropdwon li

Apr 14 2015 7:19 AM
I am binding the gridview from sqlserver using dataset and datable on PageLoad.

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

Here I am filtering the dataset on demand, then bind the gridview to it.

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

Here I am selecting dropdownlist values, then I have to show gridview records on demand.

protected void ddlPaging_SelectedIndexChanged(object sender, EventArgs e)

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


Answers (1)