Reputation: 91
I have a gridview that is set up to do paging but it is not working correctly.
Only the first page's control is visible - the other pages have boxes rendered but no control inside them.
Does anyone know why this may be? I have checked that I have more than one page of data.
Thanks,
Oliver
I have attached a screenshot which I hope illustrates my problem.
https://i.sstatic.net/NOFnB.jpg
EDIT: source for the gridview
<asp:GridView ID="GridView1" runat="server" OnPageIndexChanging="GridView1_PageIndexChanging"
CssClass="GridView1" OnSelectedIndexChanged="GridView_SelectedIndexChanged"
AllowPaging="True" PageSize="20">
<selectedrowstyle backcolor="LightCyan" forecolor="DarkBlue" font-bold="true" />
</asp:GridView>
it is populated using a dataset generated in c#
EDIT: c# codebehind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView();
}
public void bindGridView()
{
//declare the connection string to use
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
//create sql connection
SqlConnection mySQLconnection = new SqlConnection(connectionString);
//open connection
mySQLconnection.Open();
//define command using text string
SqlCommand mySqlCommand = new SqlCommand(sqlTester, mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
//create dataset to fill gridview with
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
//fill gridview
GridView1.DataSource = myDataSet;
GridView1.DataBind();
//close the sql connection
mySQLconnection.Close();
}
Upvotes: 2
Views: 2198
Reputation: 5459
You are making connection and database trip on each page index change event. It causes lot of overhead. Try this.
public void bindGridView()
{
//declare the connection string to use
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
//create sql connection
SqlConnection mySQLconnection = new SqlConnection(connectionString);
//open connection
mySQLconnection.Open();
//define command using text string
SqlCommand mySqlCommand = new SqlCommand(sqlTester, mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
//create dataset to fill gridview with
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
//fill gridview
GridView1.DataSource = myDataSet;
GridView1.DataBind();
viewstate.add("myDataSet",myDataSet);
//close the sql connection
mySQLconnection.Close();
}
In page index change event, do this... protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView.datasource=(DataTable)ViewState["myDataSet"); GridView.DataBind(); }
Use session if viewstate gives some error regarding serialization. This will make your code more efficient and optimized.
Upvotes: 0
Reputation: 11423
I think according to your aspx
and your .cs
. there is some problem in your css file"GridView1
".Try to remove the css class and tell us . is the problem still exist or not.
Note:
It's not related to your question. but i think you should separate your code in layers instead of writing all the code in your page code behind.
Read about:
Upvotes: 1