0

I have a dynamically created gridview that displays different pieces and their quantity. I want to get the total of these rows to display the total in the units column. So for example the value in the Unit column on the first row should be 190 (12 + 7 + 136 + 35)

enter image description here

The problem is the number of pieces can be different for each user. The example in the image has 13 pieces, but a different user might only have 5.

The units column will always be in the 9th position. So is there a way to check the value of each column after the unit column, till it reaches the end and then add those values together?

EDIT

 main.Columns.Add("Units", typeof(int)).SetOrdinal(8);
    foreach (DataRow row in main.Rows)
            {
                int total = 0;
                for (int i = 9; i < main.Columns.Count - 1; i++) 
                {
                    total += row[i];
                }
                row["Units"] = total;
            }
user123456789
  • 1,914
  • 7
  • 44
  • 100
  • You should be able to do something along the lines of `for (int i = 9; i < main.Columns.Count - 1; i++) total += row.Cells[i];` inside your `foreach`. This will loop over each column, starting with Units, and add its value (you'll need to cast it to the appropriate type, first). Then when you get to the end, update that row's Total Units column. – sab669 Dec 02 '15 at 15:50
  • Additionally, you can take a look at my solution to [how to get sum dynamically of datagridview column in textbox](http://stackoverflow.com/a/19963397/1189566). This is for a WinForms `DataGridView` control and not an ASP `GridView`, but the task is similar. – sab669 Dec 02 '15 at 15:52
  • @sab669 I am getting the Error 'System.Data.DataRow' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?) – user123456789 Dec 02 '15 at 15:58
  • Sorry, try `row.Item[i]` instead. I wasn't sure what type `main` was, so I assumed a `GridView`, which would give you a `GridViewRow` object which does have a `Cells` property. When in doubt, simply google "MSDN " + whatever type (in this case, `System.Data.DataRow`) and look at that type's properties to figure out what you want. – sab669 Dec 02 '15 at 16:04
  • @sab669 main is type `DataTable` so still getting the Error 'System.Data.DataRow' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?) – user123456789 Dec 02 '15 at 16:10
  • Well, something isn't right because https://msdn.microsoft.com/en-us/library/system.data.datarow(v=vs.110).aspx MSDN clearly states that `System.Data.DataRow`, which "represents a row of data in a `DataTable`", has an `Items` property which can be accessed 6 different ways. – sab669 Dec 02 '15 at 16:22
  • Looking at their example on [this page](https://msdn.microsoft.com/en-us/library/kwfe1t7z(v=vs.110).aspx) actually they access it like this: `row[int]` -- my mistake. – sab669 Dec 02 '15 at 16:23
  • @sab669 so what needs to go into the bracket? – user123456789 Dec 02 '15 at 16:34
  • The integer corresponding to the column index that you want. So like I said in my initial comment, something like `for (int i = 9; i < main.Columns.Count - 1; i++) total += row[i];` except you'll need to cast the value to an integer, or whatever `total` might be. You might also need to modify the conditions of the `for` loop itself to start at the correct column if `9` isn't right, or ignore any columns you might not want to add to your sum. – sab669 Dec 02 '15 at 16:37
  • @sab669 I added the code to my question so you can see the code so far. I'm getting the error - Error 2231 Operator '+=' cannot be applied to operands of type 'int' and 'object' – user123456789 Dec 02 '15 at 16:45
  • Yes, like I said both in my original and most recent comments, you need to convert `row[i]` to an integer. I've provided many MSDN sources to try and help you with the core problem; you need to take some initiative on your own from here man. `Convert.ToInt`, `Integer.Parse`, `Integer.TryParse`-- there's lots of options, all easily available on Google. – sab669 Dec 02 '15 at 16:50

0 Answers0