No overload for method matches delegate 'System.EventHandler

9k Views Asked by At

I am merging 2 rows in the column header of a Gridview. The row has to sort. To add the sorting ability to the column header I need to add the LinkButton control to the TableCell and then assign the Sorting method to the click event. I am getting the 'No overload for SectionGridView_Sorting...' I don't know how to add the event the the click action. This is the code:

TableCell cellSecID = new TableHeaderCell();                 
cellSecID.HorizontalAlign = HorizontalAlign.Center;                 
cellSecID.RowSpan = 2;
LinkButton lnkHeader = new LinkButton();
lnkHeader.PostBackUrl = HttpContext.Current.Request.Url.LocalPath;
lnkHeader.CommandArgument = "SectionID";
lnkHeader.Text = "SectionID";
lnkHeader.Click += new EventHandler(SectionGridView_Sorting); //This is the problem
cellSecID.Controls.Add(lnkHeader);

How to assign the Sorting method to the click event?

UPDATE This is my Sorting Method:

 protected void SectionGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
             //Get the CourseID
            populateSectionGrid();
            DataTable dtSectionGridData = SectionGridView.DataSource as DataTable;
            SectionGridViewSortExpression = e.SortExpression;

            if (dtSectionGridData != null)
            {
                DataView dataView = new DataView(dtSectionGridData);
                dataView.Sort = SectionGridViewSortExpression + " " + ConvertSectionSortDirectionToSql(e.SortDirection);

                SectionGridView.DataSource = dataView;
                SectionGridView.DataBind();
            }
        }
2

There are 2 best solutions below

0
On BEST ANSWER

After UPDATE in the question there is workaround for your issue:

Change this line:

lnkHeader.Click += new EventHandler(lnkHeader_Click);

In the event handler:

void lnkHeader_Click(Object sender, EventArgs e) 
{
    sortExpression = "yoursortexpression"; //class level string or ViewState
    SectionGridView_Sorting(null, null); //intentionally calling gridview sorting event
}

In the gridview sorting event:

protected void SectionGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
         //Get the CourseID
        populateSectionGrid();
        DataTable dtSectionGridData = SectionGridView.DataSource as DataTable;

       string sDirection = "ASC" ;

        if(sortExpression == null)
        {   
           SectionGridViewSortExpression = e.SortExpression;
           sDirection = e.SortDirection;
        } 
        else
           SectionGridViewSortExpression = sortExpression

        if (dtSectionGridData != null)
        {
            DataView dataView = new DataView(dtSectionGridData);
            dataView.Sort = SectionGridViewSortExpression + " " + ConvertSectionSortDirectionToSql(sDirection);

            SectionGridView.DataSource = dataView;
            SectionGridView.DataBind();
        }
    }

NOTE: above code is not tested. Minor tweaks for refinements may be required. Like keeping SortExpression and SortDirection and checking in the gridview sorting event. in

11
On

The signature of the event-handler method isn't compatible with the delegate type.

Presumably you are binding LinkButton.Click event with GridView Sorting Event.

 //section gridview should be like this
 protected void SectionGridView_Sorting(object sender, GridViewSortEventArgs e)
 {

 }

However you need to bind with

void lnkHeader_Click(Object sender, EventArgs e) 
{

}

If you don't have existing SectionGridView sorting event then your lnkHeader Click event should look this: (Not good practice though)

void SectionGridView_Sorting(Object sender, EventArgs e) 
{

}