-1

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?

DJGray
  • 504
  • 8
  • 26
  • yes that can be done, but stack overflow isn't supposed to be a place for "get your free solutions" so try a bit your self and come back with a real question – Thorarins Aug 26 '15 at 13:59
  • Pretty harsh response Thorarins. I have been wrestling with this for two days trying to modify code that is way above my head. The question is very real, and I am not looking for "free solutions" as you so rudely surmise. I am looking for kind guidance from someone who is not you. – DJGray Aug 26 '15 at 14:02

2 Answers2

1

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

Sabrina_cs
  • 421
  • 3
  • 18
  • Sabrina, thank you for the kind reply. I will create a DataGrid and assign it dataRows as the ItemsSource, and see what happens with that. As I noted to Thorarins above, that's been my approach for the last two days - Tweak it in some way and see what I get back. On the other suggestion: I did try to change ReadOnlyCollection to simply Collection and I got build errors all over the place. Tried to chase them all down, but finally reverted back to the starting point. I may need to run down that avenue again, and just be diligent in tracking down the issues. – DJGray Aug 26 '15 at 14:13
  • You cannot simply change a ReadOnlyCollection (that is probably a class implemented in one of your libraries by the one who implemented that code) in a Collection, but you can create a List and add to that list all elements in your ReadonlyCollection or, if the ReadOnlyCollection allows it you can use Linq and create your List with a select – Sabrina_cs Aug 26 '15 at 15:55
1

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);
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • Taffer, yes, I need to save the edits before passing dataRows to the FlatFileGenerator. I'm working Sabrina's suggestion at the moment and if I get no joy there, I'll try your "hack" above. A hack is fine if it works!! ;-) – DJGray Aug 26 '15 at 14:20