DataGrid SortCommand not firing using code-behind??

Discussion in 'ASP .Net' started by Ken Tucker, Jul 5, 2003.

  1. Ken Tucker

    Ken Tucker Guest

    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");
    }


    }
    }
    Ken Tucker, Jul 5, 2003
    #1
    1. Advertising

  2. 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" <> 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");
    > }
    >
    >
    > }
    > }
    >
    >
    Datagrid Girl [MVP], Jul 5, 2003
    #2
    1. Advertising

  3. Ken Tucker

    Ken Tucker Guest

    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]" <> wrote in message
    news:%23$...
    > 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" <> 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");
    > > }
    > >
    > >
    > > }
    > > }
    > >
    > >

    >
    >
    Ken Tucker, Jul 6, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EatHam

    SortCommand not firing

    EatHam, Jan 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    563
    EatHam
    Jan 21, 2004
  2. Ken Tucker

    DataGrid SortCommand not firing using code-behind??

    Ken Tucker, Jul 5, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    187
    Ken Tucker
    Jul 6, 2003
  3. sa siva via .NET 247

    SortCommand and PageIndexChange event not firing

    sa siva via .NET 247, Mar 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    151
    Cole Trickle
    May 5, 2005
  4. Leaf

    SortCommand not firing on DataGrid, EVENTTARGET=","

    Leaf, Jun 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    168
  5. biru
    Replies:
    2
    Views:
    220
Loading...

Share This Page