Link to home
Start Free TrialLog in
Avatar of Qw M
Qw MFlag for United States of America

asked on

Multidimensional list or array in C#

Hi,

I have a collection of data from a db like this:

Mike,insert,2013-01-02
John,update,2013-01-15
Mike,update,2013-01-03
...............................

I want to put this in a multidimensional list or array(but I do not know the number of records), and then to display the data in a grid view!

Please help me,
Thank you.
Avatar of Peter Hutchison
Peter Hutchison
Flag of United Kingdom of Great Britain and Northern Ireland image

You do need to specify the max number of records when declaring an array and arrays can only be of a certain type e.g. int, char etc.
When declaring an array esp. char or string arrays, you also need to take into consideration the string length e.g.

char myarray[100][100][25] = { {"Mike", "insert", "2013-01-02"}, {"John", "update", "2013-01-15"} , {"Mike", "update", "2013-01-03"}} ;
Avatar of Qw M

ASKER

And if I want to add to myarray vector another set of records like:
Vlad,update,2013-01-09 how can I do it. Can you write me the code!

Than you!
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@cmsxpjh
You do need to specify the max number of records when declaring an array
Well, you could repeatedly create a new array sized one larger than the previous, and then copy from the old to the new, but that gets messy. List would definitely be more "user" friendly.

When declaring an array esp. char or string arrays, you also need to take into consideration the string length e.g.
Character arrays, yes; string arrays, no.
I may suggest you to use a simple DataTable to store your db results.
It would be very simple, just get the "select" result as a DataTable with something like:
DataTable dt = null;
using (SqlDataAdapter da = new SqlDataAdapter(mySQLCommand))
{
   mySQLCommand.Connection.Open();
   dt = new DataTable(mySQLCommand.CommandText);
   da.Fill(dt);
}

Open in new window



Else, you can create a Class (as suggested by #FernandoSoto) and a "List" Class of that type. You can fill it with as many items you need and (in my opinion) is the best way to manage typed data from a db.
You can refer to this article for a more detailed description of what I mean: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/A_11874-How-to-get-a-class-out-of-a-db-query-and-serialize-deserialize-it.html
Please note that in this article it is described how to "serialize/deserialize" an Entity (a Class). You may need only a part of it, where it is showed how to create BaseEntity and BaseEntityList classes. Do it once and you get a simpler way to work...


Regards.