Sorting GridView manually from dropdown

Discussion in 'ASP .Net Web Controls' started by DBLWizard, Mar 2, 2007.

  1. DBLWizard

    DBLWizard Guest

    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
    DBLWizard, Mar 2, 2007
    #1
    1. Advertising

  2. DBLWizard

    DBLWizard Guest

    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
    DBLWizard, Mar 2, 2007
    #2
    1. Advertising

  3. DBLWizard

    DBLWizard Guest

    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
    DBLWizard, Mar 2, 2007
    #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. kurt sune
    Replies:
    4
    Views:
    412
    kurt sune
    Oct 27, 2006
  2. =?Utf-8?B?UGF0cmljayBG?=

    2 gridview questions regarding manually populate

    =?Utf-8?B?UGF0cmljayBG?=, Feb 23, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    743
    =?Utf-8?B?U3VuZGFyIE5hcmFzaW1hbg==?=
    Feb 23, 2007
  3. Replies:
    2
    Views:
    430
  4. mlg1906

    Manually sorting a GridView

    mlg1906, Jan 14, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    1,917
    bruce barker
    Jan 15, 2009
  5. jobs
    Replies:
    2
    Views:
    1,567
Loading...

Share This Page