Sorting GridView manually from dropdown

D

DBLWizard

Howdy,

I ask a question on this topic the other but in much more general
terms. Now I am trying to implment it and am having some challenges.
I have the grid populating but by binding to the dataset I loose
functionality that is putting the arrow in the sorted column. Here is
my situation:

I have a GridView that is using an ObjectDataSource. This GridView is
has headers that show which column the grid is sorting on with an
arrow indicating which way. I also have a dropdown and "refresh"
button(btnRefresh) that will allow the user to sort the grid by
selecting the desired column and direction from the drop down and
clicking refresh.

The code for the datasource looks like this -

<asp:ObjectDataSource ID="dsUpcomingPublic" runat="server"
SelectMethod="GetPublicHubConferencesByDateRange"
TypeName="Pgi.DomainLayer.HubConfRepository" EnablePaging="true"
StartRowIndexParameterName="startIndex"
MaximumRowsParameterName="maxRows"
SelectCountMethod="GetPublicConferencesCount"
SortParameterName="sortExpression">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="hubID"
SessionField="hubID" Type="Int32" />
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
</SelectParameters>
</asp:ObjectDataSource>

As you can see the Select method is specified. In the btnRefresh code
looks like this:

protected void btnRefresh_Click(object sender, EventArgs e)
{
Pgi.DomainLayer.HubConfRepository hcr = new
HubConfRepository();

// get current UTC date and then get current date time
based on timezone
DateTime currentDate = DateTime.UtcNow;
currentDate = new
timeConversion.TimeZoneConverter().convertFromGMT(currentDate,
_timeZone);
DateTime startDate = new DateTime(currentDate.Year,
currentDate.Month, currentDate.Day, 0, 0, 0);
DateTime endDate = new DateTime(currentDate.Year,
currentDate.Month, currentDate.Day, 23, 59, 59);

startDate = (new
timeConversion.TimeZoneConverter()).convertToGMT(startDate,
_timeZone);
endDate = (new
timeConversion.TimeZoneConverter()).convertToGMT(endDate, _timeZone);

int i =
Convert.ToInt16(cboSortBy.SelectedValue.Substring(0, 1));
string sortExpression = "";

sortExpression = grdUpcoming.Columns.SortExpression;

if (cboSortBy.SelectedValue.EndsWith("A"))
{
sortExpression += " ASC";
}
else if (cboSortBy.SelectedValue.EndsWith("D"))
{
sortExpression += " DESC";
}
else
{
throw new Exception("Invalid sort by Value");
}

DataSet ds =
hcr.GetPublicHubConferencesByDateRange((int)Session["hubID"],
startDate, endDate,
0, grdUpcoming.PageSize, sortExpression);

grdUpcoming.DataSourceID = null;
grdUpcoming.DataSource = ds;
grdUpcoming.DataBind();
// GetUpcomingConfs(startDate, endDate);
}

And this all works great except now the SortExpression of the grid
doesn't get set and so the code I have in the:

protected void grdUpcoming_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
int sortColumnIndex = GetSortColumnIndex();

if (sortColumnIndex != -1)
{
AddSortImage(sortColumnIndex, e.Row);
}
}
}

doesn't work right. Now I could add code to manually handle this by
keeping track of the sortExpression myselft and adding logic to this
routine to compensate for it but that sure seems to be a lot of work
for something the grid does for you if you let it.

Is there an easier way to do this?

Thanks

dbl
 
D

DBLWizard

I found another problem.

When I set the DataSourceID to null, becuase you can't have both the
DataSource and DataSourceID at the same time, I loose the
functionality of the sorting in the headers.

So how can I manually sort a GridView without disconnecting the
DataSource?

Thanks

dbl
 
D

DBLWizard

I found another problem.

When I set the DataSourceID to null, becuase you can't have both the
DataSource and DataSourceID at the same time, I loose the
functionality of the sorting in the headers.

So how can I manually sort a GridView without disconnecting the
DataSource?

Thanks

dbl
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top