Trouble reading from an ArrayList (containing data rows)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javalen7
    New Member
    • Mar 2008
    • 2

    Trouble reading from an ArrayList (containing data rows)

    I have some code that reads some datarows into an arraylist (called al), returns this to the calling function, and uses this as the DataSource property of a datagrid. I need to read what's in "al", but it won't let me specify fields. However, if I simply want to see what's in the arraylist as a whole, I can do this in the immediate window by typing ?al[0], or any other index within the range of the arraylist, and I get something like the following:
    {PVI.G_App.G_Sy stem.Applicatio n}
    ApplicationID: 1241
    ApplicationStat us: InProcess
    CreationDate: {3/15/2007 3:07:54 PM}
    G_ID: 5
    G_Name: "BYRNEJAG"
    LastUpdateDate: {3/15/2007 3:07:54 PM}
    LastUpdateUser: {075cab45-e1b3-31a3-aab8-d617a25dbah5}
    SubmissionDate: {1/1/0001 12:00:00 AM}
    Title: "CDI Enhanced Training"
    UserID: {075cab45-e1b3-31a3-aab8-d617a25dbah5}

    Since I can see the data I'm looking for, I can't figure out why I can't identify a specific field. In fact, when I try ?alStatus[0]["G_ID"], or alStatus[0][0], I get nothing. I tried alStatus[0].GetType().GetF ields(), but it returns {Dimensions:[0]}, so it's plain that I am just too new at .NET to get this to work as expected. I inherited this application, and welcome the challenge, but I'm getting frustrated that it doesn't work as I thought it would. Any advice is so appreciated.

    Thank you.

    J
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    When you get an item from an ArrayList, it comes out boxed as "object". You will need to type-cast it to its real data type "DataRow" before you can use it normally.

    So for example:
    Code:
    //dr is a DataRow
    ArrayList al = new ArrayList();
    al.Add(dr);
    //
    DataRow myDataRow = (DataRow)al[0];
    int myApplicationID=(int)myDataRow["ApplicationID"];


    As a question, why not return a DataTable instead of an ArrayList?

    Comment

    • javalen7
      New Member
      • Mar 2008
      • 2

      #3
      troubles reading arraylist solved

      Originally posted by Plater
      When you get an item from an ArrayList, it comes out boxed as "object". You will need to type-cast it to its real data type "DataRow" before you can use it normally.

      So for example:
      Code:
      //dr is a DataRow
      ArrayList al = new ArrayList();
      al.Add(dr);
      //
      DataRow myDataRow = (DataRow)al[0];
      int myApplicationID=(int)myDataRow["ApplicationID"];

      As a question, why not return a DataTable instead of an ArrayList?
      Great question. The answer is that I did think of that, but this is an inherited program, I'm new to C#, and I don't want to risk breaking something else that is depending on this same class and expects an arraylist object.

      As for your suggestion, it makes great sense, but doesn't work. I get this message:
      Code:
      Cannot cast 'alStatus[0]' (which has an actual type of 'PVI.G_App.G_System.Application') to 'System.Data.DataRow'
      Then, I try to case it as a dataset and datatable, but same thing.

      OK, I may have a solution, so I'm appending this to my last post. I type the following in the immediate window:
      Code:
      PVI.G_App.G_System.Application obj1 = (PVI.G_App.G_System.Application)alStatus[0];
      and I can then access properties using the obj1 object. I know, this is the obvious solution I should have seen at the first, but I was hoping to go directly to a dataset object. Now I have to use this to create the final object, but at least I see it's now possible.

      Thanks for all your help.

      JV
      Last edited by javalen7; Mar 6 '08, 02:24 PM. Reason: Found a solution

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Ah yes, I had thought I read that the arraylist contained DataRows, that was why I had suggested that.
        But yes, cast it to its correct object and you should be able to use all the properties of that object as normal.

        Comment

        Working...