I had something like the following working:
!!!!!!!!!! !!!!!!!!!! Start of Code snipet!!!!!!!!!! !!!!!!!!!!
<asp:DataGrid id="FormListDataGrid" runat="server"
AutoGenerateColumns="False" ShowHeader="true" HeaderStyle-CssClass="header"
AllowSorting="true" OnSortCommand="SortCurrentMonth_OnClick"
HeaderStyle-Height="25px">
<Columns>
<asp:HyperLinkColumn Runat="server" DataNavigateUrlField="FormName"
DataNavigateUrlFormatString="ShowForm.aspx?form={0}" DataTextField="FormName"
HeaderText="Form Name" SortExpression="FormName"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="Due Date" HeaderText="Due Date"
DataFormatString="{0:d}" SortExpression="Due Date"/>
<asp:BoundColumn DataField="Entity" HeaderText="Entity"
SortExpression="Entity" />
</Columns>
</asp:DataGrid>
protected void SortCurrentMonth_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
((DataView) FormListDataGrid.DataSource).Sort= e.SortExpression;
FormListDataGrid.DataBind();
}
!!!!!!!!!! !!!!!!!!!! End of Code snipet!!!!!!!!!! !!!!!!!!!!
This aspx load up the DataGrid on page start up.
However, I now created another ASPX for reporting, whose DataGrid is set to
Visible=false initially, with the DataSource, etc. only set at the click of a
button. Code-snippet as follows:
~~~~~~~~~~~~~~~Start of new Code snippet~~~~~~~~~~~~~~~
<asp:DataGrid id="ReportDataGrid" runat="server"
AutoGenerateColumns="False" ShowHeader="true"
HeaderStyle-CssClass="header" AllowSorting="true"
OnSortCommand="SortReport_OnClick" HeaderStyle-Height="25px" CellSpacing="5">
<Columns>
<asp:HyperLinkColumn Runat="server" DataNavigateUrlField="FormName"
DataNavigateUrlFormatString="ShowForm.aspx?form={0}" DataTextField="FormName"
HeaderText="Form Name" SortExpression="FormName"></asp:HyperLinkColumn>
<asp:BoundColumn runat="server" DataField="Due Date" HeaderText="Due
Date" DataFormatString="{0:d}"
SortExpression="Due Date" />
<asp:BoundColumn runat="server" DataField="Manager" HeaderText="Last
Manager" SortExpression="Manager" />
<asp:BoundColumn runat="server" DataField="DummyComments"
HeaderText="Dummy Comments" />
<asp:BoundColumn runat="server" DataField="Status" HeaderText="Status"
SortExpression="Status"/>
</Columns>
</asp:DataGrid>
private void ReportButton_Click(object sender, System.EventArgs e)
{
string reportSQL = "SELECT * FROM forms";
//Render reportSQL now based upon selected criteria on the web form
DataSet dataList = new DataSet();
OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=c:\\dev\\outstandingForms.mdb");
Conn.Open();
OdbcDataAdapter accessDataAdapter = new OdbcDataAdapter(reportSQL,Conn);
accessDataAdapter.Fill(dataList);
DataView oView= new DataView(dataList.Tables[0]);
ReportDataGrid.DataSource= oView;
ReportDataGrid.DataBind();
Conn.Close();
ReportDataGrid.Visible= true;
}
protected void SortReport_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
((DataView) ReportDataGrid.DataSource).Sort= e.SortExpression;
ReportDataGrid.DataBind();
}
~~~~~~~~~~~~~~~End of new Code snippet~~~~~~~~~~~~~~~
The issue now is whenever I try to sort something by clicking on the column
heading, I get an error, which is caused by an un-initialised
ReportDataGrid.DataSource! How could I rectify this? I don't really want to
re-run the query and re-set the DataSource, re-create the SQL everytime, do I?
"Kevin Yu [MSFT]" wrote:
> Hi Patrick,
>
> I agree with Elton that you have to assign e.SortExpression to the sort
> expression of the DataView.
>
> oView.Sort = e.SortExpression;
>
> Here is an example:
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
> frlrfsystemwebuiwebcontrolsdatagridsortcommandeventargsclasssortexpressionto
> pic.asp
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
>