# Issue

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

- Each side has different arrangement for that info.
- Each side can only hold info from tree rows, so:
- 1st, 2nd and 3rd row go on side 1;
- 4th, 5th and 6th row go on side 2;
- 7th, 8th and 9th row go on side 1;
- 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:

- ((row.Index) +1) / 3,
- round it up, with no decimals, to get an integer, (like in the above excel image),
- 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.

# 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
```

Answered By - Jon Skeet Answer Checked By - Timothy Miller (PHPFixing Admin)

## 0 Comments:

## Post a Comment

Note: Only a member of this blog may post a comment.