Manually sorting a GridView

M

mlg1906

Hello,

I have an application that uses a GridView that needs to be able to sort on
each column. The data that populates the grid is driven by data captured in 4
combo boxes and two text boxes (StartDate and EndDate). During my
btn_clickEvent I'm passing my values, returning my data as IEnumerable, and
binding it to my grid. This works fine. The problem I have occurs when I
attempt to sort my data int he grid. I've written code to handle my
GridView_Sorting event. Here's my code:

List<Claim> _Claims;
string _currentSortDirection;
string _currentSortExpression;

protected void gvClaim_Sorting(object sender, GridViewSortEventArgs e)
{
List<Claim> OrderedClaims;

e.Cancel = true;
//Check to see if the same column is clicked then we will need to
//reverse the sorting order (ex: ASC -> DESC)
if (CurrentSortExpression == e.SortExpression)
{
SetCurrentSortDirection();
}

//Check to see if the current sorting order is ascending or
descending
if (CurrentSortDirection == "ASC")
{
OrderedClaims = Claims.OrderBy
(claim => claim.GetType().InvokeMember
(CurrentSortExpression, BindingFlags.GetProperty,
null, claim, null)).ToList();
}
else
{
OrderedClaims = Claims.OrderByDescending
(claim => claim.GetType().InvokeMember
(CurrentSortExpression, BindingFlags.GetProperty,
null, claim, null)).ToList();
}

//Set the new current sort expression
CurrentSortDirection = e.SortExpression;

//Bind GridView to the ordered collection
gvClaim.DataSource = OrderedClaims;
gvClaim.DataBind();
}

/// <summary>
/// Sets our CurrentSortDirection property to the appropriate
direction
/// </summary>
void SetCurrentSortDirection()
{
switch (CurrentSortDirection)
{
case "ASC":
CurrentSortDirection = "DESC";
break;

case "DESC":
CurrentSortDirection = "ASC";
break;
}
}

public string CurrentSortDirection
{
get
{
if (ViewState["CurrentSortDirection"] == null)
{
_currentSortDirection = "ASC";
ViewState["CurrentSortDirection"] = CurrentSortDirection;
}

return (string)ViewState["CurrentSortExpression"];
}
set
{
_currentSortDirection = value;
ViewState["CurrentSortExpression"] = _currentSortDirection;
}
}

public string CurrentSortExpression
{
get
{
if (ViewState["CurrentSortExpression"] == null)
{
_currentSortExpression = "SubscriberName";
ViewState["CurrentSortExpression"] =
_currentSortExpression;
}

return (string)ViewState["CurrentSortExpression"];
}
set
{
_currentSortExpression = value;
ViewState["CurrentSortExpression"] = _currentSortExpression;
}
}

However I keep getting the following error when I attempt to sort:

An unhandled exception of type 'System.StackOverflowException' occurred in
System.Web.dll

The exception happens right here:

if (ViewState["CurrentSortDirection"] == null)

Any assistance that you can provide would be most helpful.

Thanks in advance!

Manuel
 
B

bruce barker

becuase you have an infinite recursive loop the on get:

get
{
if (ViewState["CurrentSortDirection"] == null)
{
_currentSortDirection = "ASC";

// the next statement will recurse until statck overflow


ViewState["CurrentSortDirection"] = CurrentSortDirection;
}

return (string)ViewState["CurrentSortExpression"];
}


-- bruce (sqlwork.com)
Hello,

I have an application that uses a GridView that needs to be able to sort on
each column. The data that populates the grid is driven by data captured in 4
combo boxes and two text boxes (StartDate and EndDate). During my
btn_clickEvent I'm passing my values, returning my data as IEnumerable, and
binding it to my grid. This works fine. The problem I have occurs when I
attempt to sort my data int he grid. I've written code to handle my
GridView_Sorting event. Here's my code:

List<Claim> _Claims;
string _currentSortDirection;
string _currentSortExpression;

protected void gvClaim_Sorting(object sender, GridViewSortEventArgs e)
{
List<Claim> OrderedClaims;

e.Cancel = true;
//Check to see if the same column is clicked then we will need to
//reverse the sorting order (ex: ASC -> DESC)
if (CurrentSortExpression == e.SortExpression)
{
SetCurrentSortDirection();
}

//Check to see if the current sorting order is ascending or
descending
if (CurrentSortDirection == "ASC")
{
OrderedClaims = Claims.OrderBy
(claim => claim.GetType().InvokeMember
(CurrentSortExpression, BindingFlags.GetProperty,
null, claim, null)).ToList();
}
else
{
OrderedClaims = Claims.OrderByDescending
(claim => claim.GetType().InvokeMember
(CurrentSortExpression, BindingFlags.GetProperty,
null, claim, null)).ToList();
}

//Set the new current sort expression
CurrentSortDirection = e.SortExpression;

//Bind GridView to the ordered collection
gvClaim.DataSource = OrderedClaims;
gvClaim.DataBind();
}

/// <summary>
/// Sets our CurrentSortDirection property to the appropriate
direction
/// </summary>
void SetCurrentSortDirection()
{
switch (CurrentSortDirection)
{
case "ASC":
CurrentSortDirection = "DESC";
break;

case "DESC":
CurrentSortDirection = "ASC";
break;
}
}

public string CurrentSortDirection
{
get
{
if (ViewState["CurrentSortDirection"] == null)
{
_currentSortDirection = "ASC";
ViewState["CurrentSortDirection"] = CurrentSortDirection;
}

return (string)ViewState["CurrentSortExpression"];
}
set
{
_currentSortDirection = value;
ViewState["CurrentSortExpression"] = _currentSortDirection;
}
}

public string CurrentSortExpression
{
get
{
if (ViewState["CurrentSortExpression"] == null)
{
_currentSortExpression = "SubscriberName";
ViewState["CurrentSortExpression"] =
_currentSortExpression;
}

return (string)ViewState["CurrentSortExpression"];
}
set
{
_currentSortExpression = value;
ViewState["CurrentSortExpression"] = _currentSortExpression;
}
}

However I keep getting the following error when I attempt to sort:

An unhandled exception of type 'System.StackOverflowException' occurred in
System.Web.dll

The exception happens right here:

if (ViewState["CurrentSortDirection"] == null)

Any assistance that you can provide would be most helpful.

Thanks in advance!

Manuel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top