reference a gridview column header

Discussion in 'ASP .Net' started by JohnE, Jan 1, 2010.

  1. JohnE

    JohnE Guest

    I have a gridview with column headers. These differ slightly from the
    underlying table. Example is the table has ChangeRequestStatus while
    gridview has Status. On a label below the gridview I have what is being
    sorted. Rather then the table I would prefer to have the gridview column
    header. The line below provides what the underlying datatable has.

    lblSortColumn.Text = "Sorted by: " + e.SortExpression;

    I am not getting what would be used to show the gridview column header. Any
    assistance in this is welcomed.

    Thanks.
    John
    JohnE, Jan 1, 2010
    #1
    1. Advertising

  2. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > > I am not getting what would be used to show the gridview column header.

    >
    > Add the following to the Sorting method:
    >
    > GridView gridview = (GridView)sender;
    > for (int col = 1; col < gridview.Columns.Count; col++)
    > {
    > if (gridview.Columns[col].SortExpression == e.SortExpression)
    > {
    > lblSortColumn.Text = "Sorted by: " +
    > gridview.Columns[col].HeaderText;
    > break;
    > }
    > }
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    > .
    >


    It worked. But when I go to use the paging the label reverts back to its
    original comment. The original comment is in the page load. The line is:

    lblSortColumn.Text = "Default sort: STATUS, workflow in descending order.";

    The line of code commented out below is what was originally used and it all
    worked prior with the exception of the column header name not showing in the
    lblSortColumn like I wanted. Below is the full Sorting code with your
    suggested code added.

    protected void gvwChangeRequestList_Sorting(object sender,
    GridViewSortEventArgs e)
    {
    string sort = "Asc";

    if (ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + ""
    == e.SortExpression)
    {
    if (ViewState["sortDir"] + "" == "Asc")
    sort = "Desc";
    }
    ViewState["sortExpr"] = e.SortExpression;
    ViewState["sortDir"] = sort;

    gvwChangeRequestList.DataSource = bindgrid();
    gvwChangeRequestList.DataBind();
    lblSortColumn.Visible = true;
    //lblSortColumn.Text = "Sorted by: " + e.SortExpression;

    ///////////////
    GridView gridview = (GridView)sender;
    for (int col = 1; col < gridview.Columns.Count; col++)
    {
    if (gridview.Columns[col].SortExpression == e.SortExpression)
    {
    lblSortColumn.Text = "Sorted by: " +
    gridview.Columns[col].HeaderText;
    break;
    }
    }
    ///////////////

    lblSortDirection.Visible = true;
    lblSortDirection.Text = "Sort direction: " + sort;
    TotalCount();

    }

    When the paging is used after the sort, even tho lblSortColumn reverts back
    to the default text, the sorting still works properly when paging.

    John
    JohnE, Jan 1, 2010
    #2
    1. Advertising

  3. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > > I am not getting what would be used to show the gridview column header.

    >
    > Add the following to the Sorting method:
    >
    > GridView gridview = (GridView)sender;
    > for (int col = 1; col < gridview.Columns.Count; col++)
    > {
    > if (gridview.Columns[col].SortExpression == e.SortExpression)
    > {
    > lblSortColumn.Text = "Sorted by: " +
    > gridview.Columns[col].HeaderText;
    > break;
    > }
    > }
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    > .
    >


    I got it figured out. It all works fine now. Thanks for you assistance.
    John
    JohnE, Jan 1, 2010
    #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. Keith G Hicks
    Replies:
    3
    Views:
    749
    Keith G Hicks
    Feb 18, 2008
  2. mlt
    Replies:
    2
    Views:
    812
    Jean-Marc Bourguet
    Jan 31, 2009
  3. hansiman

    Image in header column (not replacing column header text)

    hansiman, Feb 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    291
    hansiman
    Feb 7, 2004
  4. datagrid having row header and column header

    , Jul 13, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    164
  5. Teej
    Replies:
    2
    Views:
    292
Loading...

Share This Page