reference a gridview column header

  • Thread starter Thread starter JohnE
  • Start date Start date
J

JohnE

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
 
Mark Rae said:
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
 
Mark Rae said:
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
 
Back
Top