【问题标题】:No overload for method matches delegate 'System.EventHandler方法没有重载匹配委托'System.EventHandler
【发布时间】:2014-05-22 05:50:17
【问题描述】:

我正在合并 Gridview 的列标题中的 2 行。该行必须排序。要将排序功能添加到列标题,我需要将 LinkBut​​ton 控件添加到 TableCell,然后将 Sorting 方法分配给单击事件。我收到“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方法分配给点击事件?

更新 这是我的排序方法:

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

【问题讨论】:

  • 看来你正在绑定链接按钮点击事件和已经存在的 gridview_sorting 事件。
  • 你的SectionGridView_Sorting实现EventHandlerdelegate?

标签: c# gridview merge row gridview-sorting


【解决方案1】:

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

大概你正在绑定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 点击事件应该如下所示:(虽然不是好习惯)

void SectionGridView_Sorting(Object sender, EventArgs e) 
{

}

【讨论】:

  • 我在上面添加了一个现有的 SectionGridView 排序事件。我想合并这一行,使其跨越 2 行。当我这样做时,我失去了对该列进行排序的能力。我必须添加 LinkBut​​ton 才能进行排序。
  • 在合并 2 个标题行后,我需要能够对此列进行排序。我尝试设置 SortExpression 但这并不能使其可排序。
  • 我也试过这个:` lnkHeader.Click += new GridViewSortEventHandler (SectionGridView_Sorting);` 但这也没有用。
  • 这行不通。您不能将 gridview 事件绑定到链接按钮。
  • 我怎样才能让这个列排序之后我合并行标题
【解决方案2】:

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

改变这一行:

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 事件。在

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多