Hello,
Sorry for putting this problem back on the list. I had it previously under another title that now does not describe the real problem. Hopefully the new title will trigger someone that has had the same problem to respond...
The problem:
When I want to select page 2 in my grid view control, the grid view disappear.
After some trail and error (with good help from others on this network) it seems like the problem appears when the bind function is called from the event handler.
It seems that the page index is set properly, while if I store it in a session variable that is read by the function that fill in the grid on page load (FillInGridView), and then put the 'FillInGridView' function outside the 'if (!IsPostBack)' test so that it is called each time any contol causing post back is selected, the paging works.
However, I do not want to call the db each time any control is changed. Only when the next page is called.
Any ideas anybody?
Below, I've first pasted what works, then what does not work.
This works, but is a poor solution that I cannot live with:
void FillInGridView()
{
string myConnString = ConfigurationSettings.AppSettings["DSN_Fyrr"];
MySqlConnection myConnection = new MySqlConnection(myConnString);
string strSQL = "SELECT LocationName FROM PartLocationDef ORDER BY LocationName;" ;
MySqlCommand myCommand = new MySqlCommand(strSQL, myConnection);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "FailTypeDef");
g_GridCtrl.DataSource = myDataSet;
g_GridCtrl.PageIndex = (Int32)Session["GridPageIndex"];
Session["MyDataSett"] = myDataSet;
g_GridCtrl.DataBind();
myConnection.Close();
}
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["GridPageIndex"]=0;
}
FillInGridView();
}
void GridCtrl_IndexChange(object sender, GridViewPageEventArgs e)
{
g_GridCtrl.PageIndex = e.NewPageIndex;
Session["GridPageIndex"]=g_GridCtrl.PageIndex;
FillInGridView();
}
This does not work (makes the grid disappear when page 2 is selected), but would be a better solution:
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillInGridView();
}
}
void GridCtrl_IndexChange(object sender, GridViewPageEventArgs e)
{
g_GridCtrl.PageIndex = e.NewPageIndex;
g_GridCtrl.DataSource = (DataSet)Session["MyDataSett"];
g_GridCtrl.DataBind();
}