Search code examples
asp.netgridviewrefbyref

Using a ref to pass GridView columns to a method


So I'm working on this VB to C# web application migration and came across an issue that I'm hoping there is an easy work around for. There's a webform that uses the GridView control. In code, it passes the columns collection into a method that adds columns dynamically based on the user, permissions, and environment. So, the columns were passed into the function in VB using ByRef like so:

Public Sub PopulateColumns(ByRef ColumnCollection As DataControlFieldCollection)
    'Do something
End Sub

Now in C#, I've used the ref keyword, but the columns collection doesn't have a setter. What's my quickest workaround for this? I'm going to be converting this over to a jQuery grid soon so I'm not concerned with best practices, but rather just getting it to work.

Here it is in C#:

public void PopulateColumns(ref DataControlFieldCollection columnCollection)
{
    // Something here
}

which is called like this...

.PopulateColumns(ref EmployeeGridView.Columns)

Solution

  • The collection is already ByRef, so you do not need the ref argument.

    So, unless I'm having a blonde moment, you just have to do:

    public void PopulateColumns(DataControlFieldCollection columnCollection)
    {
        // Something here
    }
    
    .PopulateColumns(EmployeeGridView.Columns)
    

    Tested and working.