Re: datagrid sorting dynamic columns

Discussion in 'ASP .Net' started by avnrao, May 13, 2004.

  1. avnrao

    avnrao Guest

    did you try putting Response.End() to check if the sortGrid method is
    actually called?
    if yes, you need to bind again with latest SortExpression.

    Av.

    "jzink" <> wrote in message
    news:...
    >I have a datagrid that is populated from the results of a stored proc. I
    >now want to add sorting. Set allowSorting to true, created an event to
    >handle the sort and made sure that the event is "wired up". When I click
    >on a column header I can see a postback going back to the server. the
    >page_load event fires and I skip any processing isPostBack = true;however,
    >my sort event is never called !!! Any ideas ?
    >
    > Here is the datagrid definition:
    > <asp:datagrid id="dgAdhocReport" runat="server" CellPadding="1"
    > BorderColor="#999999" BorderWidth="1px"
    > OnItemCreated="AdhocGrid_ItemCreated" AutoGenerateColumns="False"
    > GridLines="Vertical" PageSize="15" EnableViewState="True"
    > AllowSorting="True" OnSortCommand="sortGrid"></asp:datagrid>
    >
    > Here is the sort event:
    > public void sortGrid(object source,
    > System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    > {
    > string SortExpression = e.SortExpression.ToString();
    > Response.Write("hello");
    > }
    >
    > Here is the wiring:
    > private void InitializeComponent()
    > {
    > .
    > this.dgAdhocReport.SortCommand += new
    > System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.sortGrid);
    > .
    > }
    >
    > Here is the dynamic populating of the datagrid:
    >
    > dgAdhocReport.DataSource=ds;
    > //build all cols for the datagrid dynamically based on what is in the
    > dataset
    > foreach (DataColumn col in ds.Tables[0].Columns)
    > {
    > BoundColumn dg = new BoundColumn();
    > dg.DataField = col.ColumnName;
    > dg.HeaderText = col.ColumnName;
    > dg.SortExpression = col.ColumnName;
    > dg.ItemStyle.Wrap = false;
    > dgAdhocReport.Columns.Add(dg);
    > }
    > dgAdhocReport.DataBind();
    >
    >
     
    avnrao, May 13, 2004
    #1
    1. Advertising

  2. With AutoGenerateColumns is false , are you setting the columns yourself. If
    not how and what are you seeing in the datagrid?

    --
    Swanand Mokashi
    Microsoft Certified Solution Developer (.NET)
    Microsoft Certified Application Developer (.NET)
    http://www.swanandmokashi.com/
    http://www.swanandmokashi.com/HomePage/WebServices/
    Home of the Stock Quotes, Quote of the day and Horoscope web services
    "jzink" <> wrote in message
    news:...
    > problem appears to have something to do with the AutoGenerateColumns

    property. If I set it to true the sort event fires, if set to false the
    event does not fire.
     
    Swanand Mokashi, May 13, 2004
    #2
    1. Advertising

  3. Yes. I see my data columns and they include a link that if I click should sort by that column.
    The columns are generated based on the results of a stored procedure. Here is the code for generating the columns
     
    =?Utf-8?B?anppbms=?=, May 13, 2004
    #3
  4. avnrao

    Elad Volpin Guest

    Hi jzink,

    I've also wrestled with it quite a bit, and reached my conclusions
    (sort seems to be working for me so far):

    1. If you set AutoGenerateColumns to true, the sort will fire but then
    you end up having all columns generated, which you can hide only using
    the ItemDataBound event. However, there's a bug over using this
    method, that if you click some of the sort headers, the sort will fire
    giving you the WRONG sort column (it will give you one of the auto
    generated columns headers which was set to invisible using the
    ItemDataBound event - so this solution seemed to be working ok until I
    found out about this bug - if anyone knows different I'll be happy to
    hear about it).

    2. And here's what you want to know: If you set the
    AutoGenerateColumns to false, sort will not fire, UNLESS you build
    your custom columns in the OnInit() overriden event. Unbelievable,
    right ? Well, I read it on a different thread in google
    (http://groups.google.com/groups?hl=...08&seekm=lqlMFnwhCHA.1824@cpmsftngxa08#link7).
    I don't understand this behaviour, but it seems to be working.

    I'll be happy to hear comments from someone who has figured out this
    behaviour, or whether these are known bugs.

    Hope this helps,
    Elad.
     
    Elad Volpin, Jun 6, 2004
    #4
    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. Jordo
    Replies:
    0
    Views:
    454
    Jordo
    Oct 31, 2003
  2. =?Utf-8?B?amF2YXRvcGlh?=

    Dynamic columns not sorting in DataGrid

    =?Utf-8?B?amF2YXRvcGlh?=, Oct 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    510
    =?Utf-8?B?amF2YXRvcGlh?=
    Oct 2, 2004
  3. Replies:
    5
    Views:
    599
  4. Kostia

    Dynamic columns in DataGrid, Paging/Sorting and Data Caching

    Kostia, Apr 16, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    176
    Kostia
    Apr 16, 2004
  5. javatopia

    Dynamic columns and sorting/paging in DataGrid

    javatopia, Oct 19, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    128
    javatopia
    Oct 19, 2004
Loading...

Share This Page