GridView Sorting Problem

Discussion in 'ASP .Net' started by GaryDean, Dec 16, 2008.

  1. GaryDean

    GaryDean Guest

    I have a GridView datasourced by a SQLDataSource with Enable Sorting and
    Enable Selection turned on. However I don't like the headings and all of
    the columns that come from the SQLDataSource in in the RowDataBound event I
    do...
    if (e.Row.RowType == DataControlRowType.Header)
    {
    e.Row.Cells[billmethod].Visible = false;
    e.Row.Cells[dateissued].Visible = false;
    e.Row.Cells[receiptamt].Visible = false;
    e.Row.Cells[cbmark].Text = "Mark";
    e.Row.Cells[viewinvoice].Text = "View Doc";
    e.Row.Cells[invoicenumber].Text = "Invoice #";
    e.Row.Cells[billtoorganization].Text = "Bill To Company";
    e.Row.Cells[createdate].Text = "Created";
    e.Row.Cells[billcycle].Text = "Pay Type";
    e.Row.Cells[status].Text = "Status";
    e.Row.Cells[invamt].Text = "Bill Amt";
    e.Row.Cells[amtreceived].Text = "Amt Rec";
    e.Row.Cells[orgtype].Text = "Aging";


    }

    This has apparently nuked my sorting feature because my custom headings are
    not underlined and clicking on them does not sort. They are born dead. Now
    if I hit the select button on any row, the grid shows the headings from SQL
    and the sort feature is restored. It stays restored until the next time
    RowDataBound is triggered.

    I want to have custom headings and the sort capability. Is this possible
    without changing my sql?
    Thanks,
    Gary
    GaryDean, Dec 16, 2008
    #1
    1. Advertising

  2. Hi Gary,

    My Name is Allen Chen. It's my pleasure to work with you on this issue.

    From your description, you want to use the auto generated columns of the
    GridView and to replace the default header LinkButton with your own custom
    one. Is my understanding correct?

    If so please try the following code in the RowCreated event hanlder of the
    GridView control:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Header)
    { Button b=new Button();
    b.Text = "Custom Sort Button!";
    b.CommandName="Sort";
    b.CommandArgument="theID";//the field you want to sort
    e.Row.Cells[0].Controls.Clear();
    e.Row.Cells[0].Controls.Add(b);
    }
    }

    Here a new button will replace the default LinkButton on the header of the
    first column.

    The key point is to set the CommandName and the CommandArgument. The
    CommandName is mandatory to be "Sort". It means you want to use this button
    to sort GridView. The CommandArgument represents the field you want to
    sort. You need to replace it with the real one in your case.

    Please have a try and let me know if it works. If you need further
    assistance please don't hesitate to let me know.

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Allen Chen [MSFT], Dec 17, 2008
    #2
    1. Advertising

  3. GaryDean

    GaryDean Guest

    Allen:
    Thanks for your answer.

    Actually my goal was not to replace the whole button although I tested your
    code and it did work. My goal was just to put up a more friendly text
    header than the one returned by SQL i.e. "Invoice #" instead of
    "invoicenumber". But learning from your example prompted me to use the
    following two lines of code in the RowDataBound event and this also worked:

    LinkButton b =
    (LinkButton)e.Row.Cells[invoicenumber].Controls[0];
    b.Text = "Friendly Heading";
    So, problem solved.
    Thanks,
    Gary

    "Allen Chen [MSFT]" <> wrote in message
    news:703cyT$...
    > Hi Gary,
    >
    > My Name is Allen Chen. It's my pleasure to work with you on this issue.
    >
    > From your description, you want to use the auto generated columns of the
    > GridView and to replace the default header LinkButton with your own custom
    > one. Is my understanding correct?
    >
    > If so please try the following code in the RowCreated event hanlder of the
    > GridView control:
    >
    > protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    > {
    > if (e.Row.RowType == DataControlRowType.Header)
    > { Button b=new Button();
    > b.Text = "Custom Sort Button!";
    > b.CommandName="Sort";
    > b.CommandArgument="theID";//the field you want to sort
    > e.Row.Cells[0].Controls.Clear();
    > e.Row.Cells[0].Controls.Add(b);
    > }
    > }
    >
    > Here a new button will replace the default LinkButton on the header of the
    > first column.
    >
    > The key point is to set the CommandName and the CommandArgument. The
    > CommandName is mandatory to be "Sort". It means you want to use this
    > button
    > to sort GridView. The CommandArgument represents the field you want to
    > sort. You need to replace it with the real one in your case.
    >
    > Please have a try and let me know if it works. If you need further
    > assistance please don't hesitate to let me know.
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Support
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 2 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions. Issues of this
    > nature are best handled working with a dedicated Microsoft Support
    > Engineer
    > by contacting Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    GaryDean, Dec 17, 2008
    #3
  4. Hi Gary,

    Glad to know this issue has been solved. Have a nice day!

    Regards,
    Allen Chen
    Microsoft Online Support
    Allen Chen [MSFT], Dec 18, 2008
    #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. Replies:
    2
    Views:
    1,406
    James Kanze
    Jul 6, 2010
  2. Jason
    Replies:
    0
    Views:
    372
    Jason
    Oct 4, 2006
  3. jobs
    Replies:
    2
    Views:
    1,568
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    464
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    140
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page