0

我正在尝试使用一些 C# 代码来扩展 VB.NET 项目中的 GridView 功能。我使用的代码来自这里

在 C# 代码中有一个 GroupHeader 的事件定义:

/// <summary>
/// Event triggered after a row for group header be inserted
/// </summary>
public event GroupEvent GroupHeader;

这通过上述网站上的示例进行了扩展:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper = new GridViewHelper(this.GridView1);
    helper.RegisterGroup("ShipRegion", true, true);
    helper.RegisterGroup("ShipName", true, true);
    helper.GroupHeader += new GroupEvent(helper_GroupHeader);
    helper.ApplyGroupSort();
}

private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
{
    if (groupName == "ShipRegion")
    {
        row.BackColor = Color.LightGray;
        row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;
    }
    else if (groupName == "ShipName")
    {
        row.BackColor = Color.FromArgb(236, 236, 236);
        row.Cells[0].Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + row.Cells[0].Text;
    }
}

我的问题是,如何将此代码转换为 VB.NET?

我已经像这样转换了事件实现:

Private Sub helper_GroupHeader(ByVal groupName As String, ByVal values As Object(), ByVal row As GridViewRow)
    Try
        If groupName = "ITEM#" Then
            row.BackColor = Color.LightBlue
            row.Cells(0).Text = "&nbsp;&nbsp;" & row.Cells(0).Text
        End If
    Catch ex As Exception

    End Try
End Sub

然后我怎样才能用 VB.NET 调用(引发?)这个事件?

4

2 回答 2

4

您正在寻找AddHandler

Protected Sub Page_Load(sender As Object, e As EventArgs)
    Dim helper As New GridViewHelper(Me.GridView1)
    helper.RegisterGroup("ShipRegion", True, True)
    helper.RegisterGroup("ShipName", True, True)
    AddHandler helper.GroupHeader, AddressOf helper_GroupHeader
    helper.ApplyGroupSort()
End Sub

我假设该事件是从GridViewHelper.

于 2012-04-11T12:10:45.933 回答
1

您可以使用以下方式订阅该事件AddHandler

AddHandler GroupHeader, AddressOf helper_GroupHeader

并使用以下方法引发事件RaiseEvent

RaiseEvent GroupHeader()
于 2012-04-11T11:12:33.717 回答