Link to home
Create AccountLog in
Avatar of minglelinch
minglelinch

asked on

gridview sorting problem

I set the grid AllowSorting="True" and OnPageIndexChanging="subjectGrid_PageIndexChanging" OnSorting="subjectGrid_Sorting"

I got error -
CS1656: Cannot assign to 'Sort' because it is a 'method group'
at the following line -
subjectGrid.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

I used the sorting function at http://forums.asp.net/p/956540/1177923.aspx
I copied the code as below -
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
   string newSortDirection = String.Empty;

   switch (sortDirection)
   {
      case SortDirection.Ascending:
         newSortDirection = "ASC";
         break;

      case SortDirection.Descending:
         newSortDirection = "DESC";
         break;
   }

   return newSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gridView.PageIndex = e.NewPageIndex;
   gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as DataTable;

   if (dataTable != null)
   {
      DataView dataView = new DataView(dataTable);
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

from my point of view, I don't see why you need the custom sorting method. It is doing exactly what the default behavior does. I think you can delete it and still get the same result.

other than that, I see that the syntax is ok.
Avatar of minglelinch
minglelinch

ASKER

If I only set AllowSorting to true without setting OnPageIndexChanging and OnSorting properties, I got error: Sys.WebForms.PageRequestManagerServerErrorExeption: The GridView 'Grid1' fired event Sorting which was not handled.

My grid is working fine, but sorting is not working by defaut by setting AllowSorting. I used the grid style here http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

This is my grid -
<asp:GridView ID="subjectGrid" runat="server" BackColor="#FFFFFF" ForeColor="#5078B3"
           BorderStyle="Solid" BorderColor="#5078B3" AlternatingRowStyle-BackColor="#FFFFFF"
           cellpadding="1"  cellspacing="1" AllowSorting="True" AutoGenerateColumns="False"
           DataKeyNames="subInfoId,subType,gender"
           OnRowCancelingEdit="subjectGrid_RowCancelingEdit"
           OnRowDataBound="subjectGrid_RowDataBound"
           OnRowEditing="subjectGrid_RowEditing" OnRowUpdating="subjectGrid_RowUpdating"
           OnRowCommand="subjectGrid_RowCommand" ShowFooter="True"
           OnRowDeleting="subjectGrid_RowDeleting">

How can I make the grid sorting working? Thanks.
 
Did you set the SortExpression property in the columns you want to sort? Judging from AutoGenerateColumns="False", setting this property for the columns is a must to allow the sorting to work. Set the SortExpression to the column name in the db, and hopefully the sorting will work.
Actually the grid is initially sorted by the primary key column. I want the grid is sorted by the column which the title of the column is clicked.

I have made sure each TemplateField SortExpression property is set to the column name in DB, and AutoGenerateColumns of the grid is set to false.

 When I clicked a column title, I got page error -
Error: Sys.WebForms.PageRequestManagerServerErrorException: The GridView 'subjectGrid' fired event Sorting which was not handled.
ASKER CERTIFIED SOLUTION
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I set the datasource in the following C# function, and there are multiple dropdown boxes in the grid too. I also used the C# functions to load those dropdowns. It's like hard to set SqlDataSource at front side. The grid has Add/Edit/Delete functionalities, and "None Selected" is displayed for empty value in dropdowns. Is it hard to make this grid sort working? Is it the only convienient way if I use  SqlDataSource with the grid? Thanks.
 
 private void FillSubjInfoInGrid()
    {
        DataTable dtClaim = FetchSubj(CClaimId, conn);

        if (dtClaim.Rows.Count > 0)
        {
            subjectGrid.DataSource = dtClaim;
            subjectGrid.DataBind();
        }
        else
        {
            dtClaim.Rows.Add(dtClaim.NewRow());
            subjectGrid.DataSource = dtClaim;
            subjectGrid.DataBind();

            int TotalColumns = subjectGrid.Rows[0].Cells.Count;
            subjectGrid.Rows[0].Cells.Clear();
            subjectGrid.Rows[0].Cells.Add(new TableCell());
            subjectGrid.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            subjectGrid.Rows[0].Cells[0].Text = "No Record Found";
        }
    }
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
I'll pointing the answer.
Correct. Thanks.