퇴근5분전

 

 

 이번 플젝에서 그리드에 다음과 같이 표시해야 할 일이 있었다.

 

시작점은 노란색 바탕인데 A타입은 제일 아래, B타입은 그 한칸 위에서 시작한다.

 

시계방향으로 숫자를 배치하면 된다.

 

  테스트 2는  시작위치에서 부터 한칸씩 위로 올라가면서 값을 채우고

 끝까지 올라가면서 옆으로 옮겨서 내려오면서 값을 채우며 끝난다.

 

  private void test2()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Block00");
            table.Columns.Add("Block01");

            int totalCount = 4;
            for (int loop = 0; loop < totalCount; loop++)
            {
                table.Rows.Add();
            }

            string type = "b";

            int currentCol = 0;
            int currentRow = (type == "a" ? totalCount : totalCount - 1 ) - 1;
            int startValue = 1;
            int step = -2;

            do
            {
                table.Rows[currentRow][currentCol] = startValue++;
                currentRow += step;               
                if (currentRow < 0)
                {
                    step *= -1; // 방향전환
                    currentRow = Math.Abs( currentRow - 1 ) % 2;
                    currentCol++;
                }
             
            } while (currentRow < totalCount && currentCol < 2); // 시계방향일때 종료지점
            dataGridView1.DataSource = table;
        }

 

 

 // 테스트는 위에서 부터 아래로 내려오면서 간단한 계산을 통해

 값을 채워 내려온다.  대신 타입과 홀수/짝수에 따라 시작위치가 다르다.

 노란색 시작점에서 시작이 아니고 제일 위에서부터 내려오면서 값을 채운다.

 

        private void testc()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Block00");
            table.Columns.Add("Block01");

            int totalCount = 13;
            for (int loop = 0; loop < totalCount; loop++)
            {
                table.Rows.Add();
            }

            int startcol = 0;
            int startValue = totalCount / 2 + totalCount % 2;
            string type = "b";

            int simbol = 0;

            if (type == "a")
            {
                if (totalCount % 2 == 0) // 짝
                {
                    startcol = 1;
                    simbol = 1;
                    startValue ++; // a일때 짝수는 +1
                }
                else
                {
                    startcol = 0;
                    simbol = -1;
                }
            }
            else if (type == "b")
            {
                if (totalCount % 2 == 1) // 홀
                {
                    startcol = 1;
                    simbol = 1;
               
                }
                else
                {
                    startcol = 0;
                    simbol = -1;
                 
                }
            }

            for (int loop = 0; loop < totalCount; loop++)
            {               
                startValue = (simbol * loop) + startValue;
                simbol *= -1;
                table.Rows[loop]["Block0" + startcol] = startValue;
                startcol = ( startcol + 1 ) % 2;            
            }
            dataGridView1.DataSource = table;
        }

 

       이 방법은 간단한 수의 규칙을 찾아내고 만든 것이다ㅏ.

   

A타입만 써놨지만. B타입 4위치가 우측 칸부터 시작하고 우측에 표시되는 덧셈 뺄셈값의 부호가 반대가 된다. ( -1, +2, -3, +4, -5, 6 )

 

위의 두 처리방법 모두 간단한것인긴 한데..... 그냥 적어두자.