Issues with DataGrid sort

G

Guest

I have been trying to follow some examples on
http://msdn.microsoft.com/library/d...pguide/html/cpconsortingdatainsqldatabase.asp

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

http://msdn.microsoft.com/library/d...fSystemWebUIWebControlsDataGridClassTopic.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*****************/
 
G

Guest

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
(e-mail address removed)



Patrick said:
I have been trying to follow some examples on
http://msdn.microsoft.com/library/d...pguide/html/cpconsortingdatainsqldatabase.asp

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

http://msdn.microsoft.com/library/d...fSystemWebUIWebControlsDataGridClassTopic.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*****************/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top