Help with datagrid paging in C#

C

Charlie Kunkel

I can't get my datagrid to page correctly and I think it's just the
order of events that's confusing me. What's happening as it stands
now is, I hit the Search button, which fires the btnSearch_Click
method, which sets stored procedure parameters and executes the stored
procedure then rebinds the datagrid. Everything's fine so far, but
when I click a page # at the bottom of the datagrid, it goes to page
2, but page 2 of the entire dataset, not of the dataset rebound after
btnSearch_Click fired.

here's the relevant code:



using ...

namespace Mappy
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection sqlConnection;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
protected System.Data.SqlClient.SqlDataReader sqlDataReader1;
protected System.String strSQL;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox txtProjMapName;
protected System.Web.UI.WebControls.Label lblProjMapName;
protected System.Web.UI.WebControls.Label lblRoadName;
protected System.Web.UI.WebControls.TextBox txtRoadName;
protected Mappy.DataSet1 dataSet11;
...


private void Page_Load(object sender, System.EventArgs e)

{

sqlConnection.Open();
if (!Page.IsPostBack)
{
ViewState.Add("AdvancedSearchFieldsVisible", false);

//Populate Dropdowns
System.Data.SqlClient.SqlCommand sqlCommand2 = new
System.Data.SqlClient.SqlCommand("Select * from tblMapType order by
MapType ASC",sqlConnection);

//Map Type
sqlCommand2.CommandType = CommandType.Text;
sqlCommand2.Connection = sqlConnection;
sqlDataReader1 =
sqlCommand2.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
ddlMapType.DataBind();
sqlDataReader1.Close();

//add an empty list item for when you want to search by map type =
""
ddlMapType.Items.Add(new ListItem("", ""));
ddlMapType.Items[ddlMapType.Items.Count - 1].Selected = true;


}

}




private void btnSearch_Click(object sender, System.EventArgs e)
{
//Assign values to stored procedure parameters
//RoadName
sqlDataAdapter1.SelectCommand.Parameters[1].Value =
txtRoadName.Text.ToString();
//ProjMapName
sqlDataAdapter1.SelectCommand.Parameters[2].Value =
txtProjMapName.Text.ToString();
...
BindData();

}
protected void BindData()
{
// dataSet11.Clear();
sqlDataAdapter1.Fill(dataSet11);
// dataSet11.AcceptChanges();
DataGrid1.DataSource = dataSet11.Tables[0].DefaultView;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();

}




protected void ChangeGridPage(object sender,
DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
try{
sqlDataAdapter1.Fill(dataSet11);
DataGrid1.DataBind();
}
catch (Exception ex)
{
string message;
message = ex.Message;
Label1.Text = message + "<BR><BR>" + e.NewPageIndex;

}

}




}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top