Two dimensional c# array list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agam
    New Member
    • Jan 2011
    • 21

    Two dimensional c# array list?

    Hello guys,
    I have a rather simple question.
    I have two Arraylist's and I need to add them both at the same time into a DataGridView.
    Here is the basic code I used:
    Code:
    protected ArrayList fPath = new ArrayList();
            protected ArrayList md5File = new ArrayList();
    ...
    
    foreach (string fp in fPath)
                                {
                            foreach (string md5 in md5File)
                            {
                                
                                int rw1 = dataGrid.Rows.Add();
                                int rw2 = dataGrid.Rows.Add() - 1;
                                
                                //Array file path
                                dataGrid.Rows[rw1].Cells[0].Value = fp;
                                
                                //Array md5
                                dataGrid.Rows[rw2].Cells[1].Value = md5;
                           }
    This is the only solution for inserting data to the datagridview in the same row...
    And I think that this function is quite a ram eater.
    So I deiced to make this better.
    I wanted to create a two dimensional arraylist - so it could size up dynamically.

    Code:
    protected ArrayList[,] both = new ArrayList();
    both[0,x] = fpath values
    both[2,0] = md5File values;
    How can I do that/ or do you have any better suggestions.
  • Rohit Garg
    New Member
    • Jul 2010
    • 8

    #2
    You can add an ArrayList as a single item in another ArrayList which makes the 2nd ArrayList a Multidimentiona l ArrayList

    Code:
    ArrayList both = new ArrayList();
            for (int i = 0; i < fPath.Count; i++)
            {
                ArrayList temp = new ArrayList();
                temp.Add(fPath[i]);
                temp.Add(md5File[i]);
                both.Add(temp);
            }

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Yes, I have better suggestions. First, don't use ArrayList. There's absolutely no need anymore. It was necessary in .NET 1.1, when there were no generics, but it's 2011 now. Use List<T>.

      And second, if I understand you correctly, you really don't want multidimensiona l arrays, you want one array of elements that consist of two pieces of data tied together. Create a class.

      Code:
      public class MyData
      {
        public string FilePath { get; set; }
        public string MD5Hash { get; set; }
      }
      ...
      ...
      List<MyData> list = new List<MyData>();
      ...
      Also, you should look into using the DataSource property of the DataGridView. You don't have to manually add each entry, you can just bind a list of content directly to the grid.

      Comment

      Working...