I have been trying to follow some examples on
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsortingdatainsqldatabase.asp

http://www.dotnetjunkies.com/Tutorial/E169C6D4-D335-4D2B-AE3F-918EE3161815.dcik

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassTopic.asp

I found two issues
1) Sorting does not seem to work at all when AutoGenerateColumns is set to
False
2) Data binding is not working

When AutoGenerateColumn disabled, the header appears in plain text, with no
links to sort on columns!

Even when AutoGenerate is enabled (and I can't have that!) Any suggestions
on how to resolve these?

Code snippet as follows:
/********* Start of ListForms.aspx***********************/
<asp:DataGrid id="FormListDataGrid" runat="server"
AutoGenerateColumns="False" ShowHeader="true" HeaderStyle-CssClass="header"
AllowSorting="true" OnSortCommand="SortCommand_OnClick">
<Columns>
<asp:HyperLinkColumn Runat="server" DataNavigateUrlField="FormName"
DataNavigateUrlFormatString="ShowForm.aspx?form={0}" DataTextField="FormName"
HeaderText="Form Name"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="Due Date" HeaderText="Due Date"
DataFormatString="{0:d}"/>
<asp:BoundColumn DataField="Entity" HeaderText="Entity" />
</Columns>
</asp:DataGrid>
/********* End of ListForms.aspx***********************/



/*************Start of ListForms.aspx.cs*****************/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

namespace BeanCounter
{
/// <summary>
/// Summary description for ListForms.
/// </summary>
public class ListForms : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ArchivedDataGrid;
protected System.Web.UI.WebControls.DataGrid FormListDataGrid;
protected string currentFormSQL = "SELECT * from Forms where Active=1";
protected string actualCurrentFormSql;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataSet dataList = new DataSet();
actualCurrentFormSql=currentFormSQL;

OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=c:\\db\\outstandingForms.mdb");

Conn.Open();
OdbcDataAdapter accessDataAdapter = new
OdbcDataAdapter(currentFormSQL,Conn);
accessDataAdapter.Fill(dataList);

DataView oView= new DataView(dataList.Tables[0]);
FormListDataGrid.DataSource= oView;
FormListDataGrid.DataBind();
Conn.Close();



}

#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.Load += new System.EventHandler(this.Page_Load);

}

protected void SortCommand_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
actualCurrentFormSql = currentFormSQL + " ORDER BY " + e.SortExpression;
FormListDataGrid.DataBind();
}
#endregion
}
}

/*************End of ListForms.aspx.cs*****************/

RE: Issues with DataGrid sort by EltonW

EltonW
Wed May 25 12:39:22 CDT 2005

Hi Patrick,

You should assign column property
SortExpression = "FiledName" to allow the column sortable.

What do you mean Data binding is not working?

HTH

Elton Wang
elton_Wang@hotmail.com



"Patrick" wrote:

> I have been trying to follow some examples on
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsortingdatainsqldatabase.asp
>
> http://www.dotnetjunkies.com/Tutorial/E169C6D4-D335-4D2B-AE3F-918EE3161815.dcik
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassTopic.asp
>
> I found two issues
> 1) Sorting does not seem to work at all when AutoGenerateColumns is set to
> False
> 2) Data binding is not working
>
> When AutoGenerateColumn disabled, the header appears in plain text, with no
> links to sort on columns!
>
> Even when AutoGenerate is enabled (and I can't have that!) Any suggestions
> on how to resolve these?
>
> Code snippet as follows:
> /********* Start of ListForms.aspx***********************/
> <asp:DataGrid id="FormListDataGrid" runat="server"
> AutoGenerateColumns="False" ShowHeader="true" HeaderStyle-CssClass="header"
> AllowSorting="true" OnSortCommand="SortCommand_OnClick">
> <Columns>
> <asp:HyperLinkColumn Runat="server" DataNavigateUrlField="FormName"
> DataNavigateUrlFormatString="ShowForm.aspx?form={0}" DataTextField="FormName"
> HeaderText="Form Name"></asp:HyperLinkColumn>
> <asp:BoundColumn DataField="Due Date" HeaderText="Due Date"
> DataFormatString="{0:d}"/>
> <asp:BoundColumn DataField="Entity" HeaderText="Entity" />
> </Columns>
> </asp:DataGrid>
> /********* End of ListForms.aspx***********************/
>
>
>
> /*************Start of ListForms.aspx.cs*****************/
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
> using System.Data.Odbc;
>
> namespace BeanCounter
> {
> /// <summary>
> /// Summary description for ListForms.
> /// </summary>
> public class ListForms : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.DataGrid ArchivedDataGrid;
> protected System.Web.UI.WebControls.DataGrid FormListDataGrid;
> protected string currentFormSQL = "SELECT * from Forms where Active=1";
> protected string actualCurrentFormSql;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> DataSet dataList = new DataSet();
> actualCurrentFormSql=currentFormSQL;
>
> OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
> (*.mdb)};DBQ=c:\\db\\outstandingForms.mdb");
>
> Conn.Open();
> OdbcDataAdapter accessDataAdapter = new
> OdbcDataAdapter(currentFormSQL,Conn);
> accessDataAdapter.Fill(dataList);
>
> DataView oView= new DataView(dataList.Tables[0]);
> FormListDataGrid.DataSource= oView;
> FormListDataGrid.DataBind();
> Conn.Close();
>
>
>
> }
>
> #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.Load += new System.EventHandler(this.Page_Load);
>
> }
>
> protected void SortCommand_OnClick(Object source,
> DataGridSortCommandEventArgs e)
> {
> actualCurrentFormSql = currentFormSQL + " ORDER BY " + e.SortExpression;
> FormListDataGrid.DataBind();
> }
> #endregion
> }
> }
>
> /*************End of ListForms.aspx.cs*****************/

RE: Issues with DataGrid sort by v-kevy

v-kevy
Wed May 25 22:10:09 CDT 2005

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."