Question

I need to print data from a DataGridView on both sides of a preprinted form but:

  1. Each side has different arrangement for that info.
  2. Each side can only hold info from tree rows, so:
  3. 1st, 2nd and 3rd row go on side 1;
  4. 4th, 5th and 6th row go on side 2;
  5. 7th, 8th and 9th row go on side 1;
  6. 10th, 11th and 12th go on side 2; and so on.

I will select which group to print.

I’m planning to do it this way: enter image description here

  1. ((row.Index) +1) / 3,
  2. round it up, with no decimals, to get an integer, (like in the above excel image),
  3. MOD that integer by 2, (like in the above excel image).

If the result of that MOD by 2 is 1, then it will print Side 1 arrangement, if the result of that MOD by 2 is 0, then it will print Side 2 arrangement.

  • How do I do it in C#? I'm using VS2010 Express Edition. Also, I wanted to use System.Math.Ceiling but I get a Namespace, decimal, double-precision and floating-point number warnings or errors.
Was it helpful?

Solution

I don't see that you need to use anything like that:

int zeroBasedRow = row - 1;
int side = ((zeroBasedRow / 3) % 2) + 1;

Test code:

using System;

class Test
{
    static void Main(string[] args)
    {
        for (int row = 1; row <= 12; row++)
        {
            int zeroBasedRow = row - 1;
            int side = ((zeroBasedRow / 3) % 2) + 1;
            Console.WriteLine("Row {0} goes on side {1}", row, side);
        }
    }
}

Output:

Row 1 goes on side 1
Row 2 goes on side 1
Row 3 goes on side 1
Row 4 goes on side 2
Row 5 goes on side 2
Row 6 goes on side 2
Row 7 goes on side 1
Row 8 goes on side 1
Row 9 goes on side 1
Row 10 goes on side 2
Row 11 goes on side 2
Row 12 goes on side 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top