方法的重载不匹配delegate' System.EventHandler

时间:2014-05-21 20:24:42

标签: c# gridview merge row gridview-sorting

我在Gridview的列标题中合并了2行。该行必须排序。要将排序功能添加到列标题,我需要将LinkBut​​ton控件添加到TableCell,然后将Sorting方法分配给click事件。我得到了SectionGridView_Sorting的'没有重载...'我不知道如何通过点击操作添加事件。这是代码:

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);

如何将Sorting方法分配给click事件?

更新 这是我的排序方法:

 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 个答案:

答案 0 :(得分:2)

事件处理程序方法的签名与委托类型不兼容。

据推测,您正在使用LinkButton.Click事件绑定GridView Sorting事件。

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

 }

但是你需要与

绑定
void lnkHeader_Click(Object sender, EventArgs e) 
{

}

如果您没有现有的SectionGridView排序事件,那么您的lnkHeader Click事件应该是这样的:(虽然不是很好的做法)

void SectionGridView_Sorting(Object sender, EventArgs e) 
{

}

答案 1 :(得分:1)

在问题中更新后,您的问题有解决方法:

更改此行:

lnkHeader.Click += new EventHandler(lnkHeader_Click);

在事件处理程序中:

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

在gridview排序事件中:

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();
        }
    }

注意:以上代码未经过测试。可能需要对细化进行细微调整。比如保留SortExpressionSortDirection并检入gridview sorting事件。在