Paging in Gridview not working 2nd page data not showing data? - asp.net

my gridview
<div style="margin-left: 280px">
<asp:GridView ID="exportGrdVw" runat="server" BackColor="White"
AllowPaging="True" PageSize="3"
OnPageIndexChanging="exportGrdVw_PageIndexChanging"
onpageindexchanged="exportGrdVw_PageIndexChanged">
</asp:GridView>
</div>
my code
SqlConnection con = new SqlConnection("server=acer-Pc\\Sql;database=MYDB;trusted_connection=yes");
//DataSet ds = new DataSet();
DataTable table = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter da = new SqlDataAdapter("select customername,contactno,address from employee ", con);
da.Fill(table);
BindEmployee();
}
}
public void BindEmployee()
{
exportGrdVw.DataSource = table;
exportGrdVw.DataBind();
}
protected void exportGrdVw_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
exportGrdVw.PageIndex = e.NewPageIndex;
BindEmployee();
}
the problem is gridview is displaying but when i click page 2. 2nd page data is not showing (blank). pls help me to solve this problem.
see that the code is correct or not

Use like below
SqlConnection con = new SqlConnection("server=acer-Pc\\Sql;database=MYDB;trusted_connection=yes");
//DataSet ds = new DataSet();
DataTable table = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployee();
}
}
public void BindEmployee()
{
SqlDataAdapter da = new SqlDataAdapter("select customername,contactno,address from employee ", con);
da.Fill(table);
exportGrdVw.DataSource = table;
exportGrdVw.DataBind();
}
protected void exportGrdVw_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
exportGrdVw.PageIndex = e.NewPageIndex;
BindEmployee();
}

Your datatable gets initialize in every page load so you need to get the datatable
everytime before binding it to grid.
Set your page Index before binding it to grid
So your code should be like this
public void BindEmployee(int newPageIndex = -1)
{
SqlDataAdapter da = new SqlDataAdapter("select customername,contactno,address from employee ", con);
da.Fill(table);
exportGrdVw.PageIndex = newPageIndex;
exportGrdVw.DataSource = table;
exportGrdVw.DataBind();
}

No need to call database on exportGrdVw_PageIndexChanging. Just declare DataTable table as static

You can try this:
GridView1.PageIndex = e.NewPageIndex;
SqlCommand cmd = new SqlCommand("Select * from Emp_Data ORDER BY [ID] DESC", con);
SqlDataAdapter DA1 = new SqlDataAdapter(cmd);
DA1.Fill(DT1);
GridView1.DataSource = DT1;
GridView1.DataBind();

Related

Fill a grid view by a drop down list

I have a drop down list ; a sqldatasource and a gridview .
I want to fill the grid view by the choice in the drop down list.
How can i implement this fill ?
Thanks
My question is not a duplicate of this Click
because it want to populate a drop down list inside the grid view instead my question is : by a choice of a drop down list , fill a grid view.
EDIT (Because i am in negative rep.) :
What i have tried
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
string query = "SELECT top 10 * FROM Customers";
SqlCommand cmd = new SqlCommand(query);
gvCustomers.DataSource = GetData(cmd);
gvCustomers.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
aspx page
<asp:DropDownList runat="server" ID="drp" AutoPostBack="true" OnSelectedIndexChanged="drp_SelectedIndexChanged"></asp:DropDownList>
<asp:GridView runat="server" ID="grd" AutoGenerateColumns="true"></asp:GridView>
code behind of aspx
protected void drp_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
grd = dataSource;
grd.DataBind();
}
Bind grid on selection index change event of dropdown.

How to get a table on a new web page when a button is Clicked

This is my one tag:
<asp:Button ID="button" runat="server" Text="ShowOrder" onclick="newTab" />
This is my 'aspx.cs' which will be called when button is clicked
protected void newTab(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?id="+txtSearchCustomerByID.Value);
}
What I want is to print my sql table on loaded web tab (new page) when it gets loaded.
My stored procedure is displaying the data of my table where id is equal to "id entered by user in textbox".
Now,
protected void Page_Load(object sender, EventArgs e)
{
int id_no = int.Parse(Request.QueryString["id"]);
if (Page.IsPostBack)
{
showOrders(id_no);
}
}
Now what should I have in 'Default2.aspx' so that I will get my table by using,
public void showOrders(int id)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
SqlCommand cmd = new SqlCommand("showOrdersSP", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#id", id);
con.Open();
.......
}
}
I need to use DataTable
So simply when I clicked, I will get data table on new page
You can refer below code:
public void showOrders(int id)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
DataTable dt = new DataTable();
SqlParameter[] p1 = new SqlParameter[1];
p1[0] = new SqlParameter("#id", id);
dt= getRecords_table("showOrdersSP", p1); // you will get your DataTable here
}
}
// Common Method for FillYour Tables
private DataTable getRecords_table(string spname, SqlParameter[] para)
{
string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["connName"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(connectionstring);
SqlDataAdapter ad = new SqlDataAdapter(spname, con);
ad.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
ad.SelectCommand.Parameters.AddRange(para);
con.Open();
ad.Fill(dt);
con.Close();
return dt;
}
Hope it will helps you
Thanks

!IsPostBack returns always true

I am trying to implement search operation for my gridview.When I load my page first time !IsPostBack works fine but when I click on the search button my page loads again and !IsPostBack returns true value.So, I was not able to perform my search operation
this is my code.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
((Label)Master.FindControl("Label1")).Text = (string)Session["sname"];
fillData();
}
}
public void fillData()
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM EBR_Supplier where DeleteFlag=" + 0 + " ORDER BY RowId ASC", con);
SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
fillData();
}
protected void search_Click(object sender, EventArgs e)
{
string id = txtid.Text;
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM EBR_Supplier where DeleteFlag=" + 0 + " and SupplierId='" + id + "' ORDER BY RowId ASC", con);
SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
I have found the solution myself I have a form control on my master page.I removed that it helped me.I thank you all for giving time to my question.

dynamic ActiveTabChanged for AJax Tab container

How to fire ActiveTabChanged for TabContainer. I have written a code to populate the TabPanel HeaderText from the database and this is my code
public partial class dynamicTab : System.Web.UI.Page
{
string strCon = ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString.ToString();
TabContainer ajxTab;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// createTab();
pnlDynamic.Controls.Add(ajxTab);
ajxTab_ActiveTabChanged(ajxTab, EventArgs.Empty);
}
}
private void ajxTab_ActiveTabChanged(TabContainer ajxTab, EventArgs eventArgs)
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select * from Employee where DeptID='" + ajxTab.ActiveTab.ID.ToString() + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView grd = new GridView();
grd.AutoGenerateColumns = true;
grd.DataSource = ds;
grd.DataBind();
pnlDynamic.Controls.Add(grd);
}
protected void page_init(object sender, eventargs e)
{
createtab();
}
private void createTab()
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select DeptID,DepartmentName from Department", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
ajxTab = new AjaxControlToolkit.TabContainer();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TabPanel pnl = new TabPanel();
pnl.HeaderText = ds.Tables[0].Rows[i]["DepartmentName"].ToString();
pnl.ID = ds.Tables[0].Rows[i]["DeptID"].ToString();
ajxTab.Tabs.Add(pnl);
ajxTab.ActiveTabIndex = 0;
}
}
}
For the first time ActiveTabChanged fired perfectly , but when I click on the second tab ActiveTabChanged is not getting fired. I tried by setting ajxTab.AutoPostBack=true but the tab container is not getting visible when the event occurs.
This is my sample output
On clicking computers I would like to load grid with those details so can some one help me
You can bind the content of each tab in the OnActiveTabChanged event
OnActiveTabChanged="TabContainer1_ActiveTabChanged"
AutoPostBack="true"

Using DropDown_SelectedIndexChanged to bind gridview, but paging is not working

I have a DropDownList which is having few list items. Every time I select any item, based on it I am binding a gridview, hence resulting gridview depends on my selection. Some results are exceeding the pagesize hence I need to navigate on 2nd page in GridView, however the GridView is disappeating when I am selecting 2nd page.
protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationSettings.AppSettings["ConnectionInfo"];
SqlConnection con = new SqlConnection(constr);
con.Open();
string str = string.Empty;
str = "SELECT * FROM Table1";
SqlDataAdapter adp = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
adp.Fill(ds, "myds");
BusinessKPIGrid.DataSource = ds;
BusinessKPIGrid.DataBind();
}
protected void OnGridViewPaging(object sender, GridViewPageEventArgs e)
{
//I know I need to bind the gridview here, but how ?
BusinessKPIGrid.PageIndex = e.NewPageIndex;
BusinessKPIGrid.DataBind();
}
I don't know how to bind the gridview in paging event, since I do not have any separate binding function.
there is a separate function for page event for grid view..create separate method for binding grid & call that method on pageindexchange event..
protected void BusinessKPIGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BusinessKPIGrid.PageIndex = e.NewPageIndex;
bindyourgrid();
}
try this
protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationSettings.AppSettings["ConnectionInfo"];
SqlConnection con = new SqlConnection(constr);
con.Open();
string str = string.Empty;
str = "SELECT * FROM Table1";
SqlDataAdapter adp = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
adp.Fill(ds, "myds");
ViewState["ds"]=ds;
BusinessKPIGrid.DataSource = ds;
BusinessKPIGrid.DataBind();
}
protected void OnGridViewPaging(object sender, GridViewPageEventArgs e)
{
//I know I need to bind the gridview here, but how ?
BusinessKPIGrid.PageIndex = e.NewPageIndex;
BusinessKPIGrid.DataSource = (DataSet)ViewState["ds"];
BusinessKPIGrid.DataBind();
}
From DDL_SelectedIndexChanged if you are searching some thing then use 'PageIndexChanging' and set the page size for it
protected void gvworker_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvworker.PageIndex = e.NewPageIndex;
//result got from ddl list i.e bind your data set
}

Resources