views:

569

answers:

1

Hey Guys, I have a function that returns a 2 dimensional array. Due to the nature of the program I MUST declare it as an object. Like this

object o1 = function(x);    //note the function returns something similar to this {string[x,y]};

ultimately I want to bind this data to a GridView.
If the array is one dimensional...the following method works fine.

gridView.DataSource = o1;

gridView.DataBind();

it doesn't work for multidimensional arrays.

I WOULD use the following code to turn the array of strings into a DataTable

DataTable dt = new DataTable();

dt.Columns.Add("Name", Type.GetType("System.String"));

dt.Columns.Add("Age", Type.GetType("System.String"));

dt.Columns.Add("Sex", Type.GetType("System.String"));  

for (int i = 0; i < 5; i++)
{

    dt.Rows.Add();

    dt.Rows[dt.Rows.Count - 1]["Name"] = o1[i, 0];

    dt.Rows[dt.Rows.Count - 1]["Age"] = o1[i, 1];

    dt.Rows[dt.Rows.Count - 1]["Sex"] = o1[i, 2];

}

GridMultiD.DataSource = dt;

GridMultiD.DataBind();

the problem is obviously that the object is not actually an array...but contains an array. Is it possible to access it? All the Google hits I get are people asking how to make object arrays...

Any ideas to simplify the situation are also greatly appreciated, thanks!

-Dave

note: the array is being returned by a DCOM server... so it MUST be returned as an object...I think...

+1  A: 

If the return type of the function is object but it's actually returning an array of strings, then can you not simply cast the return value?

string[,] array = (string[,]) o1;
Gary McGill
thank you so much, this solved it! I didn't realize you could cast something as an array. :P(I'm pretty new to this)
Dave