469,646 Members | 1,136 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

DataGrid SortCommand not firing using code-behind??

I've read about this issue in many articles across the net... But haven't
found a solution. I see all kinds of custom code to perform sorting with
datagrids, but my example is so simple, I must just be missing something.

Basically, I have a .aspx page that is very simple and has a simple
datagrid. All code is in the .cs page for the datagrid (hence, I'm doing all
datagrid work programmatically). The datagrid is populated from a SQL table
and prints just fine in the browser. I can click on any of the headers (for
sorting) but it appears that once clicking, the sortgrid function isn't
firing and I'm getting no results (empty page mostly) back.

I **HAVE** gotten this to work when I move all the datagrid "code" into the
aspx page, but since this is a template I'm using across many pages, I want
to put it all in the code-behind and make it totally dynamic on the calling
page... I refuse to give up and move away from the code-behind. ;)

I have several debug areas in here, one of which just does response.writes,
and another that changes a label so I know what has fired. I never ever get
anything from SortGrid (both response.write and label) to show in the
resulting HTML, so it appears to never get called when a column is clicked
on (resulting from SortCommand, see below).

The problem must lie in this line:
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);
because that's the spot where it tells what to do with the event... But I
can't figure it out for the life of me.

Thanks for any help you can provide.... Links, etc!

-Ken
Here's the code.

****************
kbresults.aspx:
****************

<%@ Page language="c#" Src="kbresults.aspx.cs" Inherits="test.kb"
enableViewState="True" AutoEventWireup="True"%>

<html>
<head></head>

<body>

<form runat="server" id=form1 name=form1>
<asp:DataGrid id="myDataGrid" runat="server"/>
</form>

<asp:Label id=testola Text="Default" runat=server/>

</body>
</html>
****************
kbresults.aspx.cs
****************

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
public class kb : System.Web.UI.Page
{

public DataGrid myDataGrid;
protected Label testola;
public DataSet ds;

private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid("");

if (IsPostBack)
{
Response.Write ("PostBack");
}
}

public void BindGrid(string sortField)
{
//debug text
Response.Write ("BindGrid");

//debug label
testola.Text = "BindGrid";

SqlConnection dsConn = new SqlConnection("blah blah");

dsConn.Open();

SqlDataAdapter dsAdapter = new SqlDataAdapter("SELECT blah from blah'",
dsConn);

DataSet ds = new DataSet();
dsAdapter.Fill(ds, "TABLE");

DataView source = new DataView(ds.Tables["TABLE"]);
//source.Sort = sortField;

myDataGrid.DataSource = source;

myDataGrid.AutoGenerateColumns = false;

myDataGrid.AllowSorting = true;
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);

// Add columns
BoundColumn col1 = new BoundColumn();
col1.DataField = "Category";
col1.SortExpression = "Category";
col1.HeaderText = "Category";
myDataGrid.Columns.Add(col1);

BoundColumn col2 = new BoundColumn();
col2.DataField = "ID";
col2.SortExpression = "ID";
col2.HeaderText = "ID";
myDataGrid.Columns.Add(col2);

HyperLinkColumn col3 = new HyperLinkColumn();
col3.DataNavigateUrlField = "ID";
col3.DataNavigateUrlFormatString = "blah.asp";
col3.DataTextField = "Title";
col3.SortExpression = "Title";
col3.HeaderText = "Title";
myDataGrid.Columns.Add(col3);

BoundColumn col4 = new BoundColumn();
col4.DataField = "DateUpdated";
col4.SortExpression = "DateUpdated";
col4.HeaderText = "Date";
myDataGrid.Columns.Add(col4);

BoundColumn col5 = new BoundColumn();
col5.DataField = "EnteredBy";
col5.SortExpression = "EnteredBy";
col5.HeaderText = "Entered By";
myDataGrid.Columns.Add(col5);

myDataGrid.DataBind();
}

protected void SortGrid(Object src, DataGridSortCommandEventArgs e)

{
//debug label
testola.Text = "You sorted";

//BindGrid((string)e.SortExpression);

//debug text
Response.Write ("SortGrid");
}
}
}
Nov 17 '05 #1
2 8149
Hi Ken,
Since you're creating your columns dynamically, try changing this line:

if (!IsPostBack) BindGrid("");

To:
BindGrid("");

Datagrid Girl
http://www.datagridgirl.com

"Ken Tucker" <ke*@iit.net> wrote in message
news:i8GNa.43612$926.4506@sccrnsc03...
I've read about this issue in many articles across the net... But haven't
found a solution. I see all kinds of custom code to perform sorting with
datagrids, but my example is so simple, I must just be missing something.

Basically, I have a .aspx page that is very simple and has a simple
datagrid. All code is in the .cs page for the datagrid (hence, I'm doing all datagrid work programmatically). The datagrid is populated from a SQL table and prints just fine in the browser. I can click on any of the headers (for sorting) but it appears that once clicking, the sortgrid function isn't
firing and I'm getting no results (empty page mostly) back.

I **HAVE** gotten this to work when I move all the datagrid "code" into the aspx page, but since this is a template I'm using across many pages, I want to put it all in the code-behind and make it totally dynamic on the calling page... I refuse to give up and move away from the code-behind. ;)

I have several debug areas in here, one of which just does response.writes, and another that changes a label so I know what has fired. I never ever get anything from SortGrid (both response.write and label) to show in the
resulting HTML, so it appears to never get called when a column is clicked
on (resulting from SortCommand, see below).

The problem must lie in this line:
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid); because that's the spot where it tells what to do with the event... But I
can't figure it out for the life of me.

Thanks for any help you can provide.... Links, etc!

-Ken
Here's the code.

****************
kbresults.aspx:
****************

<%@ Page language="c#" Src="kbresults.aspx.cs" Inherits="test.kb"
enableViewState="True" AutoEventWireup="True"%>

<html>
<head></head>

<body>

<form runat="server" id=form1 name=form1>
<asp:DataGrid id="myDataGrid" runat="server"/>
</form>

<asp:Label id=testola Text="Default" runat=server/>

</body>
</html>
****************
kbresults.aspx.cs
****************

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
public class kb : System.Web.UI.Page
{

public DataGrid myDataGrid;
protected Label testola;
public DataSet ds;

private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid("");

if (IsPostBack)
{
Response.Write ("PostBack");
}
}

public void BindGrid(string sortField)
{
//debug text
Response.Write ("BindGrid");

//debug label
testola.Text = "BindGrid";

SqlConnection dsConn = new SqlConnection("blah blah");

dsConn.Open();

SqlDataAdapter dsAdapter = new SqlDataAdapter("SELECT blah from blah'",
dsConn);

DataSet ds = new DataSet();
dsAdapter.Fill(ds, "TABLE");

DataView source = new DataView(ds.Tables["TABLE"]);
//source.Sort = sortField;

myDataGrid.DataSource = source;

myDataGrid.AutoGenerateColumns = false;

myDataGrid.AllowSorting = true;
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);
// Add columns
BoundColumn col1 = new BoundColumn();
col1.DataField = "Category";
col1.SortExpression = "Category";
col1.HeaderText = "Category";
myDataGrid.Columns.Add(col1);

BoundColumn col2 = new BoundColumn();
col2.DataField = "ID";
col2.SortExpression = "ID";
col2.HeaderText = "ID";
myDataGrid.Columns.Add(col2);

HyperLinkColumn col3 = new HyperLinkColumn();
col3.DataNavigateUrlField = "ID";
col3.DataNavigateUrlFormatString = "blah.asp";
col3.DataTextField = "Title";
col3.SortExpression = "Title";
col3.HeaderText = "Title";
myDataGrid.Columns.Add(col3);

BoundColumn col4 = new BoundColumn();
col4.DataField = "DateUpdated";
col4.SortExpression = "DateUpdated";
col4.HeaderText = "Date";
myDataGrid.Columns.Add(col4);

BoundColumn col5 = new BoundColumn();
col5.DataField = "EnteredBy";
col5.SortExpression = "EnteredBy";
col5.HeaderText = "Entered By";
myDataGrid.Columns.Add(col5);

myDataGrid.DataBind();
}

protected void SortGrid(Object src, DataGridSortCommandEventArgs e)

{
//debug label
testola.Text = "You sorted";

//BindGrid((string)e.SortExpression);

//debug text
Response.Write ("SortGrid");
}
}
}

Nov 17 '05 #2
Thanks. I'm surprised that works, but it looks like it was just a case of
not really understanding the flow of execution. Now when I sort, it's off by
a click (the first click doesn't sort, the next one sorts on the last
click). I'm close though.

I assumed code would execute immediate at SortGrid, but now that I think
about it, it must start at Page_Load, go through DataGrid, then fire at
SortGrid...

Thanks again for your help.

Ken

"Datagrid Girl [MVP]" <da**********@operamail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Ken,
Since you're creating your columns dynamically, try changing this line:

if (!IsPostBack) BindGrid("");

To:
BindGrid("");

Datagrid Girl
http://www.datagridgirl.com

"Ken Tucker" <ke*@iit.net> wrote in message
news:i8GNa.43612$926.4506@sccrnsc03...
I've read about this issue in many articles across the net... But haven't found a solution. I see all kinds of custom code to perform sorting with
datagrids, but my example is so simple, I must just be missing something.
Basically, I have a .aspx page that is very simple and has a simple
datagrid. All code is in the .cs page for the datagrid (hence, I'm doing

all
datagrid work programmatically). The datagrid is populated from a SQL

table
and prints just fine in the browser. I can click on any of the headers

(for
sorting) but it appears that once clicking, the sortgrid function isn't
firing and I'm getting no results (empty page mostly) back.

I **HAVE** gotten this to work when I move all the datagrid "code" into

the
aspx page, but since this is a template I'm using across many pages, I

want
to put it all in the code-behind and make it totally dynamic on the

calling
page... I refuse to give up and move away from the code-behind. ;)

I have several debug areas in here, one of which just does

response.writes,
and another that changes a label so I know what has fired. I never ever

get
anything from SortGrid (both response.write and label) to show in the
resulting HTML, so it appears to never get called when a column is clicked on (resulting from SortCommand, see below).

The problem must lie in this line:
myDataGrid.SortCommand += new

DataGridSortCommandEventHandler(SortGrid);
because that's the spot where it tells what to do with the event... But I can't figure it out for the life of me.

Thanks for any help you can provide.... Links, etc!

-Ken
Here's the code.

****************
kbresults.aspx:
****************

<%@ Page language="c#" Src="kbresults.aspx.cs" Inherits="test.kb"
enableViewState="True" AutoEventWireup="True"%>

<html>
<head></head>

<body>

<form runat="server" id=form1 name=form1>
<asp:DataGrid id="myDataGrid" runat="server"/>
</form>

<asp:Label id=testola Text="Default" runat=server/>

</body>
</html>
****************
kbresults.aspx.cs
****************

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
public class kb : System.Web.UI.Page
{

public DataGrid myDataGrid;
protected Label testola;
public DataSet ds;

private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid("");

if (IsPostBack)
{
Response.Write ("PostBack");
}
}

public void BindGrid(string sortField)
{
//debug text
Response.Write ("BindGrid");

//debug label
testola.Text = "BindGrid";

SqlConnection dsConn = new SqlConnection("blah blah");

dsConn.Open();

SqlDataAdapter dsAdapter = new SqlDataAdapter("SELECT blah from blah'", dsConn);

DataSet ds = new DataSet();
dsAdapter.Fill(ds, "TABLE");

DataView source = new DataView(ds.Tables["TABLE"]);
//source.Sort = sortField;

myDataGrid.DataSource = source;

myDataGrid.AutoGenerateColumns = false;

myDataGrid.AllowSorting = true;
myDataGrid.SortCommand += new

DataGridSortCommandEventHandler(SortGrid);

// Add columns
BoundColumn col1 = new BoundColumn();
col1.DataField = "Category";
col1.SortExpression = "Category";
col1.HeaderText = "Category";
myDataGrid.Columns.Add(col1);

BoundColumn col2 = new BoundColumn();
col2.DataField = "ID";
col2.SortExpression = "ID";
col2.HeaderText = "ID";
myDataGrid.Columns.Add(col2);

HyperLinkColumn col3 = new HyperLinkColumn();
col3.DataNavigateUrlField = "ID";
col3.DataNavigateUrlFormatString = "blah.asp";
col3.DataTextField = "Title";
col3.SortExpression = "Title";
col3.HeaderText = "Title";
myDataGrid.Columns.Add(col3);

BoundColumn col4 = new BoundColumn();
col4.DataField = "DateUpdated";
col4.SortExpression = "DateUpdated";
col4.HeaderText = "Date";
myDataGrid.Columns.Add(col4);

BoundColumn col5 = new BoundColumn();
col5.DataField = "EnteredBy";
col5.SortExpression = "EnteredBy";
col5.HeaderText = "Entered By";
myDataGrid.Columns.Add(col5);

myDataGrid.DataBind();
}

protected void SortGrid(Object src, DataGridSortCommandEventArgs e)

{
//debug label
testola.Text = "You sorted";

//BindGrid((string)e.SortExpression);

//debug text
Response.Write ("SortGrid");
}
}
}


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by xrow | last post: by
2 posts views Thread by enak | last post: by
1 post views Thread by gerry | last post: by
7 posts views Thread by DC Gringo | last post: by
2 posts views Thread by Stuart | last post: by
2 posts views Thread by saleek | last post: by
1 post views Thread by Arun | last post: by
1 post views Thread by Alex Maghen | last post: by
3 posts views Thread by Mark Gilkes | last post: by
1 post views Thread by Mark Gilkes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.