Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Sorting GridView manually from dropdown

Reply
Thread Tools

Sorting GridView manually from dropdown

 
 
DBLWizard
Guest
Posts: n/a
 
      03-02-2007
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" />
<asparameter Name="startDate" Type="DateTime" />
<asparameter 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(s tartDate,
_timeZone);
endDate = (new
timeConversion.TimeZoneConverter()).convertToGMT(e ndDate, _timeZone);

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

sortExpression = grdUpcoming.Columns[i].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)Sessio n["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

 
Reply With Quote
 
 
 
 
DBLWizard
Guest
Posts: n/a
 
      03-02-2007
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

 
Reply With Quote
 
 
 
 
DBLWizard
Guest
Posts: n/a
 
      03-02-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Manually sorting a GridView mlg1906 ASP .Net 1 01-15-2009 03:36 AM
1 Gridview. Dropdown A is column from database, Dropdown B is column from database, Data in A and B must be from same row. anonymoushamster@gmail.com ASP .Net 2 11-07-2007 12:40 PM
2 gridview questions regarding manually populate =?Utf-8?B?UGF0cmljayBG?= ASP .Net 1 02-23-2007 04:00 PM
Manually sorting a gridview, am I plain stupid? kurt sune ASP .Net 4 10-27-2006 12:36 PM
Gridview sorting event - Gridview Datasource always Null .. why??? jobs ASP .Net Web Controls 2 10-24-2006 07:08 AM



Advertisments