Qw M
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.
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.
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!
Vlad,update,2013-01-09 how can I do it. Can you write me the code!
Than you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@cmsxpjh
You do need to specify the max number of records when declaring an arrayWell, 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:
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-exchang e.com/Prog ramming/La nguages/.N ET/Visual_ CSharp/A_1 1874-How-t o-get-a-cl ass-out-of -a-db-quer y-and-seri alize-dese rialize-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.
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);
}
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-exchang
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.
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"}} ;