简体   繁体   中英

how to get all values from Looping a dynamic List in c#?

i have a dynamic List like this :

dynamic[,] Tablevalues = {
                         { 1       ,"FirstName"        , 11111111111111 },
                         { 2       ,"SecondName"       , 22222222222222 }, 
                         { 3       ,"ThirdName"        , 33333333333333 }
                         };

and i want to loop it so i can add it later in my .Mdf DataBase , and what i tried so far is :

for (int i=0; i<Tablevalues.Length; i++){

      // ----- output just for test ------
      MessageBox.Show(  Tablevalues[i, i].ToString()  );             
      } 

but the result of " Tablevalues[i, i] " only show me :

1  ,           ,   
   ,SecondName , 
   ,           ,33333333333333
  • so how to output all the Values ?
for (int i = 0; i < Tablevalues.Length; i++) {
    MessageBox.Show(Tablevalues[i, i].ToString());
}

You only have one loop variable, so you're only looping from 0, 0 , 1, 1 ... up to Length - 1, Length - 1 (ie the diagonal.)

Use two for -loops, along with GetLength(dimension) to get the rows/columns.

Something like:

for (int i = 0; i < Tablevalues.GetLength(0); i++) {
    for (int j = 0; j < Tablevalues.GetLength(1); j++) {
        MessageBox.Show(Tablevalues[i, j].ToString());
    }
}

Because you need two for for 2d array:

for (int i = 0; i < Tablevalues.GetLength(0); i++)
{
  for (int j = 0; j < Tablevalues.GetLength(1); j++)
  {
    MessageBox.Show(Tablevalues[i, j].ToString());
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM