Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multidimensional Arrays error in fill IList Interface

Multidimensional Arrays error in fill IList Interface

Scheduled Pinned Locked Moved C#
helpcssdatabase
19 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M michael nabil

    Thank you for your response. buz i load csv file in grid view after that i want to convert this data to multidimensional arrays

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #10

    That's a bad way to go. Controls should NEVER be used as your storage for data. Your CSV should be parsed to a data structure designed to hold that data. Fields can be setup to hold strings, integers, doubles, or whatever you need, and the CSV parser just places the values in the appropriate fields with no conversion. The DGV can then be used to display it if needed. Using the DGV as your storage makes accessing that data slow because you're now doing a bunch of converting to sting or other types that you wouldn't need to do otherwise. For example, this is garbage. You're converting a value in the DGV to a string and then parsing that string as an integer. This code will also fail if you've got no value in that cell.

    invoiceline.quantity = int.Parse(datavalu[i, 5].ToString());

    Putting the data directly into a datastructure designed to hold it from the CSV parser is a much better way to go as you get to validate the data before you use or even display it and handle that occurrence at a more appropriate time, when it's being loaded.

    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
    Dave Kreskowiak

    M 1 Reply Last reply
    0
    • M michael nabil

      public class InvoiceLine
      {
      public string description { get; set; }
      public string itemType { get; set; }
      public string itemCode { get; set; }
      public string internalCode { get; set; }
      public string unitType { get; set; }
      public int quantity { get; set; }
      public UnitValue unitValue { get; set; }
      public int salesTotal { get; set; }
      public double valueDifference { get; set; }
      public double totalTaxableFees { get; set; }
      public Discount discount { get; set; }
      public int netTotal { get; set; }
      public double itemsDiscount { get; set; }
      public IList taxableItems { get; set; }
      public int total { get; set; }
      }

      I'm sorry i forget to attach the class i didn't understand your answer "Because you only have one instance of the class and you insert it multiple times into the list. And each time round the loop you overwrite the values you set in the previous instance." if i have 10 records in grid it means 10 instance to insert one invoice sorry i can't under stand your example about phone and car

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #11

      OK. You have a car. It's a red Ford. I have a car. It's a black Mercedes. We go for a drive in my car. What colour is it?

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        That's a bad way to go. Controls should NEVER be used as your storage for data. Your CSV should be parsed to a data structure designed to hold that data. Fields can be setup to hold strings, integers, doubles, or whatever you need, and the CSV parser just places the values in the appropriate fields with no conversion. The DGV can then be used to display it if needed. Using the DGV as your storage makes accessing that data slow because you're now doing a bunch of converting to sting or other types that you wouldn't need to do otherwise. For example, this is garbage. You're converting a value in the DGV to a string and then parsing that string as an integer. This code will also fail if you've got no value in that cell.

        invoiceline.quantity = int.Parse(datavalu[i, 5].ToString());

        Putting the data directly into a datastructure designed to hold it from the CSV parser is a much better way to go as you get to validate the data before you use or even display it and handle that occurrence at a more appropriate time, when it's being loaded.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        M Offline
        M Offline
        michael nabil
        wrote on last edited by
        #12

        You're absolutely right but I'm trying code and my problem isn't with grid view but my problem is with how to fill invoicelines right i try another way to show invoicelines in listBox1 and run good

        string[,] datavalu = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

                foreach(DataGridViewRow row in dataGridView1.Rows)
                {
        
        
                    foreach (DataGridViewColumn col in dataGridView1.Columns)
                    {
                        datavalu\[row.Index, col.Index\] = dataGridView1.Rows\[row.Index\].Cells\[col.Index\].Value.ToString();
        
                    }
                }
                int i = 0;
        
                foreach(string ss in datavalu)
                {
        
                    listBox1.Items.Add(ss);
                    i++;
                }
        

        rustle after that i change code to this but run wrong

        for ( i = 0; i < datavalu.GetLength(0); i++)
        {

                    invoiceline2.description = datavalu\[i, 0\].ToString();
                    invoiceline2.itemType = datavalu\[i, 1\].ToString();
                    invoiceline2.itemCode = datavalu\[i, 2\].ToString();
                    invoiceline2.internalCode = datavalu\[i, 3\].ToString();
                    invoiceline2.unitType = datavalu\[i, 4\].ToString();
                    invoiceline2.quantity = int.Parse(datavalu\[i, 5\].ToString());
        
                    invoicelines.Add(invoiceline);
                    
                    
                }
        

        some thing i miss can you help me

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          OK. You have a car. It's a red Ford. I have a car. It's a black Mercedes. We go for a drive in my car. What colour is it?

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          M Offline
          M Offline
          michael nabil
          wrote on last edited by
          #13

          black

          OriginalGriffO 1 Reply Last reply
          0
          • M michael nabil

            black

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #14

            Right. Obviously! So you understand that "car" is a generic word, but that each vehicle that is a "car" is separate and distinct. "My car" is not the same as "your car" - it shares some generic characteristics such as the number of wheels, and that it has an engine (or engines) but most of it's attributes are specific to a particular vehicle. In computing, we call "car" a class and a specific vehicle an instance of that class, with the properties of an instance being defined by the class but "filled in" by the instance:

            class Car
            {
            Color Color {get; set;}
            Manufacturer Manunfacturer {get; set;}
            }
            ...
            Car myCar = new Car() { Color = Colors.Black, Manufacturer = Manufacturers.Mercedes};
            Car yourCar = new Car() { Color = Colors.Red, Manufacturer = Manufacturers.Ford};

            If you respray yourCar blue, it doesn't affect the colour of myCar - you know that! So if your code creates just one instance of an InvoiceLine, what happens if you set the properties over and over again?

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            M 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Right. Obviously! So you understand that "car" is a generic word, but that each vehicle that is a "car" is separate and distinct. "My car" is not the same as "your car" - it shares some generic characteristics such as the number of wheels, and that it has an engine (or engines) but most of it's attributes are specific to a particular vehicle. In computing, we call "car" a class and a specific vehicle an instance of that class, with the properties of an instance being defined by the class but "filled in" by the instance:

              class Car
              {
              Color Color {get; set;}
              Manufacturer Manunfacturer {get; set;}
              }
              ...
              Car myCar = new Car() { Color = Colors.Black, Manufacturer = Manufacturers.Mercedes};
              Car yourCar = new Car() { Color = Colors.Red, Manufacturer = Manufacturers.Ford};

              If you respray yourCar blue, it doesn't affect the colour of myCar - you know that! So if your code creates just one instance of an InvoiceLine, what happens if you set the properties over and over again?

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              M Offline
              M Offline
              michael nabil
              wrote on last edited by
              #15

              thank for you help but i think it's hard create instance to each new line in invoice lines first i used this code to see result

              string[,] datavalu = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

                      foreach(DataGridViewRow row in dataGridView1.Rows)
                      {
              
              
                          foreach (DataGridViewColumn col in dataGridView1.Columns)
                          {
                              datavalu\[row.Index, col.Index\] = dataGridView1.Rows\[row.Index\].Cells\[col.Index\].Value.ToString();
              
                          }
                      }
                      int i = 0;
              
                      foreach(string ss in datavalu)
                      {
              
                          listBox1.Items.Add(ss);
                          i++;
                      }
              

              I think I thought wrong from the start. Often there are simpler ways Do you know a better way

              OriginalGriffO 1 Reply Last reply
              0
              • M michael nabil

                thank for you help but i think it's hard create instance to each new line in invoice lines first i used this code to see result

                string[,] datavalu = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

                        foreach(DataGridViewRow row in dataGridView1.Rows)
                        {
                
                
                            foreach (DataGridViewColumn col in dataGridView1.Columns)
                            {
                                datavalu\[row.Index, col.Index\] = dataGridView1.Rows\[row.Index\].Cells\[col.Index\].Value.ToString();
                
                            }
                        }
                        int i = 0;
                
                        foreach(string ss in datavalu)
                        {
                
                            listBox1.Items.Add(ss);
                            i++;
                        }
                

                I think I thought wrong from the start. Often there are simpler ways Do you know a better way

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #16

                Quote:

                i think it's hard create instance to each new line in invoice lines

                Why? You do it pretty easily when you create the one you do use! So why do you think it is going to be harder to create them when you actually need them?

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                M 1 Reply Last reply
                0
                • M michael nabil

                  Hello everyone, my project get data from dataGridView after that convert rows to Multidimensional Arrays and i want to assign Multidimensional Arrays to IList Interface but the IList Interface fill last row in gridview every time my code

                  public IList invoiceLines { get; set; }

                  InvoiceLine invoiceline = new InvoiceLine();
                  string[,] datavalu = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

                          foreach (DataGridViewRow row in dataGridView1.Rows)
                          {
                  
                  
                              foreach (DataGridViewColumn col in dataGridView1.Columns)
                              {
                                  datavalu\[row.Index, col.Index\] = dataGridView1.Rows\[row.Index\].Cells\[col.Index\].Value.ToString();
                  
                              }
                          }
                          int i ;
                        
                          for ( i = 0; i < datavalu.GetLength(0); i++)
                          {
                             
                              invoiceline.description = datavalu\[i, 0\].ToString();
                              invoiceline.itemType = datavalu\[i, 1\].ToString();
                              invoiceline.itemCode = datavalu\[i, 2\].ToString();
                              invoiceline.internalCode = datavalu\[i, 3\].ToString();
                              invoiceline.unitType = datavalu\[i, 4\].ToString();
                              invoiceline.quantity = int.Parse(datavalu\[i, 5\].ToString());
                  
                              invoicelines.Add(invoiceline);
                              
                              
                          }
                  

                  my data

                  description itemType itemCode internalCode unitType quantity
                  Food EGS EG-538486562-1314 1314 EA 1
                  Food2 EGS EG-538486562-1315 1500 EA 5

                  my problem is that invoicelines return 2 rows same data in last row in the grid

                  Food2 EGS EG-538486562-1315 1500 EA 5

                  Can anyone tell me why please Thanks in advance

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #17

                  Find whoever put "Food2" in there and stone them.

                  Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Quote:

                    i think it's hard create instance to each new line in invoice lines

                    Why? You do it pretty easily when you create the one you do use! So why do you think it is going to be harder to create them when you actually need them?

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    M Offline
                    M Offline
                    michael nabil
                    wrote on last edited by
                    #18

                    thanks a lot

                    for ( i = 0; i < datavalu.GetLength(0); i++)
                    {
                    InvoiceLine invoiceline2 = new InvoiceLine();
                    invoiceline2.description = datavalu[i, 0].ToString();
                    invoiceline2.itemType = datavalu[i, 1].ToString();
                    invoiceline2.itemCode = datavalu[i, 2].ToString();
                    invoiceline2.internalCode = datavalu[i, 3].ToString();
                    invoiceline2.unitType = datavalu[i, 4].ToString();
                    invoiceline2.quantity = int.Parse(datavalu[i, 5].ToString());

                                invoicelines.Add(invoiceline2);
                                
                                
                            }
                    

                    my mistake was

                    InvoiceLine invoiceline = new InvoiceLine();

                    create instants before loop

                    OriginalGriffO 1 Reply Last reply
                    0
                    • M michael nabil

                      thanks a lot

                      for ( i = 0; i < datavalu.GetLength(0); i++)
                      {
                      InvoiceLine invoiceline2 = new InvoiceLine();
                      invoiceline2.description = datavalu[i, 0].ToString();
                      invoiceline2.itemType = datavalu[i, 1].ToString();
                      invoiceline2.itemCode = datavalu[i, 2].ToString();
                      invoiceline2.internalCode = datavalu[i, 3].ToString();
                      invoiceline2.unitType = datavalu[i, 4].ToString();
                      invoiceline2.quantity = int.Parse(datavalu[i, 5].ToString());

                                  invoicelines.Add(invoiceline2);
                                  
                                  
                              }
                      

                      my mistake was

                      InvoiceLine invoiceline = new InvoiceLine();

                      create instants before loop

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #19

                      That's the one. Though I'd have just moved the original line into the loop! :-D

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups