I thought I had solved it but was wrong...I'm a bit of a beginner so I'm not
sure I'll be using the correct terminology...Any help is VERY appreciated.
When I initialise all the columns in the Page_Init event the SortCommand
events seem to get 'registered' for all columns. Then I remove the columns I
don't need using the tabstrip's selectedindex property in the Page_Load.
In this instance I have 10 columns, 3 for the first tab, 4 for the second
and 3 for the last tab. So the first tab will only show columns 0-2, second
tab shows columns 3-6 and the last tab shows columns 7-9.
When I navigate to the second tab, extract data I need, bind data to
datagrid and click the sortcommand button the first column in the displayed
datagrid's column collection has a SortExpression of 'y' (this is column 3)
but in the SortCommand event the e.SortExpression is 'x' which is the
SortExpression of column 0 of all 10.
Here is the code with a lot of stuff removed to try and make it clearer...
public class Search : UserInterfaceLayer.LineIntBasePage {
//Control Definitions
protected System.Web.UI.WebControls.DataGrid dgResults;
private void Page_Init(object sender, EventArgs e) {
// Initialise Datagrid
this.InitDataGrids();
}
private void Page_Load(object sender, System.EventArgs e) {
// Remove columns not required for current view...
this.FormatDataGrids();
}
private void Page_PreRender(object sender, EventArgs e) {
if(this.ResultsDataSource != null){
//Now Sort the datagrids
SortGridData((ICollection)this.ResultsDataSource);
// Now bind the data
this.dgResults.DataSource = this.ResultsDataSource; //ResultDataSource
stored in Session
this.dgResults.DataBind();
this.dgResults.Visible = true;
}
}
//Web Form Designer generated code
#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.dgResults.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgResults_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new EventHandler(this.Page_PreRender);
this.Init += new EventHandler(Page_Init);
}
#endregion
//Event Handlers
private void dgResults_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e) {
Trace.Write("SORTCOMMAND", e.SortExpression);
this.SortField = e.SortExpression;
}
/// <summary>
/// Create all the columns required by the DataGrid
/// </summary>
private void InitDataGrids(){
Trace.Write("INITDATAGRIDS", "start of init: column count = " +
this.dgResults.Columns.Count);
// Initialise
HyperLinkColumn hc = null;
this.dgResults.Columns.Clear();
// Set up the columns
// Members
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Member ID";
hc.SortExpression = "MemberID";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderText = "Email";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Name";
hc.SortExpression = "Surname";
this.dgResults.Columns.Add(hc);
// Courses
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Course Reference";
hc.SortExpression = "CourseReference";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Title";
hc.SortExpression = "Title";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Version";
hc.SortExpression = "Version";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Live";
hc.SortExpression = "Live";
this.dgResults.Columns.Add(hc);
// Course Instances
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Course Instance Title";
hc.SortExpression = "CourseTitle";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Tutor";
hc.SortExpression = "Tutor";
this.dgResults.Columns.Add(hc);
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Start Date";
hc.SortExpression = "CourseDate";
this.dgResults.Columns.Add(hc);
// Set up the search results pager style
this.dgResults.PagerStyle.Mode = PagerMode.NumericPages;
this.dgResults.PagerStyle.PageButtonCount =
PageHelper.DG_PAGEBUTTONCOUNT;
Trace.Write("INITDATAGRIDS", "end of init: column count = " +
this.dgResults.Columns.Count);
}
/// <summary>
/// Remove the columns that are not
/// required by the currently selected tab
/// </summary>
private void FormatDataGrids(){
const int MEMLOWER = 0; // Lower Column Index Boundary
const int MEMUPPER = 2; // Member Upper Column Index Boundary
const int COURSEUPPER = 6; // Course Upper Column Index Boundary
const int CIUPPER = 9; // Course Instance Upper Column Index Boundary
Trace.Write("FORMAT", this.tbsSearch.SelectedIndex.ToString());
// Set up the columns
switch(this.tbsSearch.SelectedIndex){
case 0: // Members
for(int i = CIUPPER; i > MEMUPPER; i--){
this.dgResults.Columns.RemoveAt(i);
}
break;
case 1: // Courses
for(int i = CIUPPER; i > COURSEUPPER; i--){
this.dgResults.Columns.RemoveAt(i);
}
for(int i = MEMUPPER; i >= MEMLOWER; i--){
this.dgResults.Columns.RemoveAt(i);
}
break;
case 2: // Course Instances
for(int i = COURSEUPPER; i >= MEMLOWER; i--){
this.dgResults.Columns.RemoveAt(i);
}
break;
}
Trace.Write("FORMAT", "end of FormatDataGrids: column count = " +
this.dgResults.Columns.Count + ", sort column 0 = " +
this.dgResults.Columns[0].SortExpression );
}
private void SortGridData(ICollection list) {
Trace.Write("SORTGRIDDATA", this.tbsSearch.SelectedIndex + ", " +
this.SortField);
// Set up the columns
switch(this.tbsSearch.SelectedIndex){
case 0: // Members
// Initialise
MemberCollection.MemberFields sortMem =
MemberCollection.MemberFields.InitValue;
switch(this.SortField) {
case "MemberID":
sortMem = MemberCollection.MemberFields.Id;
break;
case "Surname":
sortMem = MemberCollection.MemberFields.Surname;
break;
}
((MemberCollection)list).Sort(sortMem, this.SortAscending);
break;
case 1: // Courses
// Initialise
CourseCollection.CourseFields sortCo =
CourseCollection.CourseFields.InitValue;
switch(this.SortField){
case "CourseReference":
sortCo = CourseCollection.CourseFields.CourseReference;
break;
case "Title":
sortCo = CourseCollection.CourseFields.Title;
break;
case "Live":
sortCo = CourseCollection.CourseFields.Live;
break;
case "Version":
sortCo = CourseCollection.CourseFields.Version;
break;
}
((CourseCollection)list).Sort(sortCo, this.SortAscending);
break;
case 2: // Course Instances
// Initialise
CourseInstanceCollection.CourseInstanceFields sortCI =
CourseInstanceCollection.CourseInstanceFields.InitValue;
switch(this.SortField){
case "CourseReference":
sortCI =
CourseInstanceCollection.CourseInstanceFields.CourseReference;
break;
case "CourseTitle":
sortCI =
CourseInstanceCollection.CourseInstanceFields.CourseTitle;
break;
case "CourseDate":
sortCI =
CourseInstanceCollection.CourseInstanceFields.CourseDate;
break;
case "Tutor":
sortCI =
CourseInstanceCollection.CourseInstanceFields.Tutor;
break;
}
((CourseInstanceCollection)list).Sort(sortCI, this.SortAscending);
break;
}
}
#endregion
}