.NCF에서는 DataGridView가 없고, DataGrid만이 있습니다.

이 DataGrid를 그릴 때 각각의 Cell 형태는 TextBox의 형태를 띄므로, 왼쪽 정렬이 기본 값입니다.

 

먼저, TextBox에서 오른쪽 정렬을 구현하려면.

- 희안하게도 TextBox 기본값에서는 Left 정렬 밖에 없습니다.

- 속성에서 Multiline을 true로 줘야지만 Center, Right 값을 선택할 수 있습니다.

- 그런고로 원하는 정렬을 설정 후, Enter 값을 먹지 않도록 하는 방식을 사용합니다.

- 이 Enter 입력을 FormPreView에서 KeyUp으로 막습니다.

참고 링크

 

이와 마찬가지로 DataGrid의 TextBox에서 Center, Right 정렬을 시키려면, Multiline을 true로 줘야 합니다. 

 

다음과 같은 메소드를 구성하여 DataTable의 내부 Column들을 조절하였습니다.


private void TableStyleSetting(DataTable _DT) {
  Dictionary<string, int> set = new Dictionary<string, int> {
      {"컬럼 이름", 100} // 컬럼 길이 셋팅
  };

  dataGrid.TableStyles.Clear(); // 현 dataGrid의 스타일 제거

  DataGridTableStyle tableStyle = new DataGridTableStyle();
  tableStyle.MappingName = _DT.TableName;
  foreach (DataColumn item in _DT.Columns) {
    DataGridCustomTextBoxColumn Cell = new DataGridCustomTextBoxColumn();
    Cell.Owner = _DT;
    Cell.Width = set[item.ColumnName];
    Cell.MappingName = item.ColumnName;
    Cell.HeaderText = item.ColumnName;
    Cell.ReadOnly = true;

      string[] currency = new string[] { "수정할 컬럼 이름들" };
      foreach (string curr in currency) {
        if (curr.Equals(item.ColumnName)) {
          Cell.Format = "c"; // Text 포멧, 여기서는 화페 포멧을 예시로 하였다.
          Cell.Alignment = HorizontalAlignment.Right; // 오른쪽 정렬
        }
      }
      
    tableStyle.GridColumnStyles.Add(Cell);
 }
 dataGrid.TableStyles.Add(tableStyle); // 현 dataGrid에 설정한 스타일 추가
}

 

  여기서 DataGridCutomTextColumn을 구현하려면 몇몇 추가 클래스들이 있어야 합니다. 

  이 문제를 해결하는데 제가 참고하였던 이 페이지에서, 7718-DataGridCustomColumns.zip을 받아 DataGridCustomColumnBase와 DataGridCustomTextBoxcolumn을 구현하시면 됩니다.

 

  다만 구현 하실 때, 다음 부분도 수정해주셔야 합니다.

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush) {
    Brush background = backBrush;

    //if ((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum)) {
    //    background = _alternatingBrush; 
    //}

    g.FillRectangle(background, bounds);
}

  위의 코드는 DataGridCustomColumnBase에 있는 각각의 셀 배경을 그리는 DrawBackground 메소드인데, 위와 같이 주석처리를 해 주셔야 정상 작동합니다. if 문 안의 내용은 홀수 행마다 배경색을 변경하는 예를 넣어놓았는데, 그대로 작동할 경우 홀수 행이 정상적으로 나오지 않습니다.

+ Recent posts