Click here to Skip to main content
15,918,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am fairly new to C# and am having trouble with populating a DataGrid. I can pupulate it just fine if my array is hardcoded with values and I call the method to build the datagrid.

_dataArray1.Add(new double[] {32, 45, 76567, 567, 878});


When I try to populate using a single dimension array and then call the method I recieve an error message that

MSIL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Below is an excerpt of the code... I have been trying to debug this for almost 2 days... any help is appreciated.
Thanks!


THIS IS WHAT I AM TRYING:
{INSIDE BUTON CLICK
C#
 _names.Add("CardNum");
double[] junkarray = { 32, 45, 76567, 567, 878 };

 int z = 0;
while (z < junkarray.Length)
 {

     _dataArray1.Add(new double[] {junkarray[z]});
     z++;


dataGridView1.DataSource = GetResultsTable();



}END OF BUTTON CLICK

      public DataTable GetResultsTable()
        {
            // Create the output table.
            DataTable d = new DataTable();
            // The current process name.
       
            // Loop through all process names.
            for (int i = 0; i < this._dataArray1.Count; i++)
            {
                // The current process name.
                string name = this._names[i];
           
                // Add the program name to our columns.
                d.Columns.Add(name);
                // Add all of the memory numbers to an object list.
                List<object> objectNumbers = new List<object>();
                // Put every column's numbers in this List.
                foreach (double number in this._dataArray1[i])
                {
                    objectNumbers.Add((object)number);
                }
                // Keep adding rows until we have enough.
                while (d.Rows.Count < objectNumbers.Count)
                {
                    d.Rows.Add();
                }
                // Add each item to the cells in the column.
                for (int a = 0; a < objectNumbers.Count; a++)
                {
                    d.Rows[a][i] = objectNumbers[a];
                }
            }
            return d;
        }



 }
Posted

I'm about half asleep, but shouldn't the line:
_dataArray1.Add(new double[] {junkarray[z]});

instead be:
_dataArray1.Add(junkarray[z]);
 
Share this answer
 
string name = this._names[i];

I only see one Add to this array. Since you are using _dataArray1.Count to restrict you for loop, the index will almost immediately be outside the _names array.

If you run this through the debugger, the exception will highlight the line the error is on. Look at the values on the line, and it should become obvious. This is a skill it is well worth spending some time on: it will save you lots later!
 
Share this answer
 
Comments
Espen Harlinn 11-Jan-11 14:58pm    
5+ For spotting this - like Marc & Marcus I kept looking at the code above "END OF BUTTON CLICK", it looks so odd that I just about forgot the actual question :)
What you are doing in the hard-coded one that you say works results in a _dataArray1 object that looks like this. (Note, the {} indicates that the underlying type is an array.)
[0] = {32, 45, 76567, 567, 878}

After your while loop you would have
[0] = {32}
[1] = {45}
[2] = {76567}
[3] = {567}
[4] = {878}


As Mark's answer above states, the way to solve is by assigning the value in the while like this
_dataArray1.Add( junkarray[z] );
 
Share this answer
 
v3
Comments
WhiskeyBusiness 11-Jan-11 17:56pm    
Thank you everyone for your help. I am trying to debug this as much as possible on my own but am am at my wits end. I tried using

_dataArray1.Add( junkarray[z] );

returns an ERROR that Argument '1': cannot convert fromt double to double[]

However when I send the list over to the method as such using

_dataArray1.Add(new double[] {junkarray[z]});


The datagridview will only display the first element. I checked using the debugger and there are 5 elements in the array being sent to the method.
WhiskeyBusiness 11-Jan-11 18:16pm    
For some reason the foreach loop does not want to dump out the array. Any ideas ? Thanks again everyone for your help!

foreach (double number in this._dataArray1[i])
{

objectNumbers.Add((object)number);
}
JOAT-MON 11-Jan-11 19:42pm    
@RajxC - Each of the array elements in this._dataArray1 are double[] that contain one element. When you say foreach(double number in this._dataArray1[i]) you are telling it to iterate through the double values in the double[] located in the 'i' index of this._dataArray1...there is only one value in that array at that index.

When you use the while loop to assign values from junkarray, you are splitting up the array and creating five new arrays with one element in each. What you can do to mimic the hard coded way is to assign like this: _dataArray1.Add(junkarray) <-- notice I did not specify an index.
WhiskeyBusiness 11-Jan-11 20:24pm    
Ahh ok... wonderful thanks Joat-MON and Marcus, Mark and OriginalGriff for your help!
Espen Harlinn 16-Jan-11 9:16am    
5+ If he was so grateful, why didn't he vote?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900