Update a ReadOnlyCollection in a GridView

247 Views Asked by At

I am trying to modify some code written by another gentleman that generates flat files in various formats. The particular flat file in question is one that is positionally defined - Value X is at position 10, Value Y at 17, and value Z at 26, etc.

When the user clicks the "Create File" button, this extract gets called:

ReadOnlyCollection<IDataRow> dataRows = null;
try
{
    dataRows = FlatFileExporter.MerrillLynch.GetMerrillLynchData(mPostSeq, mPaycheckDate, mPayrollStartDate, mPayrollEndDate);
}
catch (Exception ex)
{
    Response.Write((ex.InnerException != null ? ex.InnerException.Message + "; " : "") + ex.Message);
}

At this point, I know I have this ReadOnlyCollection named dataRows. It was populated from a Stored Procedure "GetMerrillLynchData" in this routine:

public static ReadOnlyCollection<IDataRow> GetMerrillLynchData(string postSeq, DateTime paycheckDate, DateTime payrollStartDate, DateTime payrollEndDate)
{
    string dateFormat = "MM/dd/yyyy";
    string query = string.Format(
        "exec GatherMerrillLynchPayrollData {0}, '{1}', '{2}', '{3}'",
        "'" + postSeq + "'",
        paycheckDate.ToString(dateFormat),
        payrollStartDate.ToString(dateFormat),
        payrollEndDate.ToString(dateFormat)
    );

    return FileGenerator.GetDataRows(query, MerrillLynchMeta.Columns, dict => new MerrillLynchDataRow(dict));
}

The ReadOnlyCollection will be fed to a FlatFileGenerator which will output the properly formatted file.

The Problem: Our Payroll Admin wants to be able to make edits to that file before it is FTP'd to Merrill Lynch. I am wondering if it is possible to open and edit dataRows (maybe in a GridView?) before it is sent to the FlatFileGenerator. Then after she is done editing I'd like to save her edits right back to dataRows and continue with the code as it is written.

Can this be done?

2

There are 2 best solutions below

2
On

If the ReadOnlyCollection is a collection of Datarows, just casted to the IDataRow interface which is probably a subset of what a Datarow class does, You can probably use the collection as ItemsSource in a Datagrid and se what you get. After that I presume you have to set the datagrid to allow edits and the content of the collection will be modified. To help you more we need to know how is the IDataRow interface made and what type of base collection is used for the ReadOnlyCollection. If the collection is really ReadOnly, to edit it you probably need to put its content in a not readonly collection, and use it to edit the data, then create a new ReadOnlyCollection from the modified data and use that to create the file. HTH

1
On

Solution 1:

Make a read-write copy of data rows:

return new List<IDataRow>(dataRows);

Of course, if you need to save the altered collection, you must to do it manually.

Solution 2:

If the ReadOnlyCollection wraps a non-trivial BL collection object, which is writable and maintains the changes whenever the collection is updated, then you might want to extract the original collection. This is actually a hack, and is not a nice solution at all:

FieldInfo wrappedCollection = dataRows.GetType().GetField("list",
    BindingFlags.Instance | BindingFlags.NonPublic);
return (IList<IDataRow>)wrappedCollection.GetValue(dataRows);