EDIT: I would suggest the following to fix the issue:
Change your Page_Load
method to this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Substance_Master.Attributes["CurrentSortField"] = "Substance_ID";
Substance_Master.Attributes["CurrentSortDirection"] = "ASC";
BindGridViewData();
}
}
And change the Substance_Master_PageIndexChanging
method to this:
protected void Substance_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Substance_Master.PageIndex = e.NewPageIndex;
var sortField = Substance_Master.Attributes["CurrentSortField"];
var sortDirection = Substance_Master.Attributes["CurrentSortDirection"];
if (sortField != null && sortDirection != null)
{
Substance_Master.DataSource = Substance_MasterAccessLayer.GetAllSubstances(sortField + "" + sortDirection);
}
Substance_Master.DataBind();
}
Explanation: You need to set the datasource of GridView before databind in PageIndexChanging event method. And you may need to sort the datasource depending on the current sort conditions.