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();
   }
}
LVL 1
minglelinchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iHadiCommented:
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.
minglelinchAuthor Commented:
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.
 
iHadiCommented:
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.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

minglelinchAuthor Commented:
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.
iHadiCommented:
How are you setting the datasource? I suggest you use a SqlDataSource with the grid

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
minglelinchAuthor Commented:
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";
        }
    }
Tom KnowltonWeb developerCommented:
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.
minglelinchAuthor Commented:
I'll pointing the answer.
minglelinchAuthor Commented:
Correct. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.