2011-09-05 1 views
1

일부 내 DataGridViewCellsGetClipboardContent 메서드에서 잘못된 값을 반환합니다. 셀 DataGridViewComboBoxColumn에있는 셀이므로 셀의 value 속성이 아닌 표시된 속성을 사용합니다. 가치 그 자체를 돌려주고 싶습니다.DataGridViewCell.GetClipboardContent 재정의 - 형식 구체화를 지원 (다시 구현)해야합니까?

초기 시도는 단순히 내 DataGridViewComboBoxCell 후손에

Protected Overrides Function GetClipboardContent(ByVal rowIndex As Integer, ByVal firstCell As Boolean, ByVal lastCell As Boolean, ByVal inFirstRow As Boolean, ByVal inLastRow As Boolean, ByVal format As String) As Object 
    Return Value 
    End Function 

을 사용했지만, 나는이 방법은 모든 데이터 형식 DataGridView에가있는 표준에 의해 지원 한번, 셀 값 당 한 번 이상이라고 주목 format = "HTML", "Text", "UnicodeText"및 "Csv".

csv의 경우 기본 구현은 마지막 셀이 아닌 경우 쉼표를 추가합니다. html의 경우 표/표 행의 첫 번째/마지막 행/셀인지에 따라 올바른 태그가 추가됩니다. 형식 특정, 셀 값이 아닙니다.

그래서 을 클립 보드 으로 대체하면 해당 형식 관련 측면을 모두 다시 구현하지 않아도됩니까? 그러면 기본 클래스에 이미 존재하는 기능에 대한 코드가 생성됩니다. 그렇지 않습니까?

답변

1

짧은 대답 : 핸들 CellFormatting, 클립 보드 "값 가져 오기"중에 포맷팅이 요청 된 경우 표시 값 대신 형식이 지정되지 않은 값을 반환하십시오. 이것을 감지하려면 combobox 열이 combobox 셀 자손을 만들어서 GetClipboardContent 메서드가 호출 될 때 열의 속성에 플래그를 설정합니다.

긴 대답 :

protected virtual object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) { 
      if (DataGridView == null) 
      return null; 

     if (rowIndex < 0 || rowIndex >= DataGridView.RowCount) 
      throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values."); 

     string value = null; 

     if (Selected) { 
      DataGridViewCellStyle style = GetInheritedStyle (null, rowIndex, false); 
      value = GetEditedFormattedValue (rowIndex, DataGridViewDataErrorContexts.ClipboardContent | DataGridViewDataErrorContexts.Formatting) as string; 
     } 

     if (value == null) 
      value = string.Empty; 

     string table_prefix = string.Empty, cell_prefix = string.Empty, row_prefix = string.Empty; 
     string table_suffix = string.Empty, cell_suffix = string.Empty, row_suffix = string.Empty; 

     if (format == DataFormats.UnicodeText || format == DataFormats.Text) { 
      if (lastCell && !inLastRow) 
       cell_suffix = Environment.NewLine; 
      else if (!lastCell) 
       cell_suffix = "\t"; 
     } else if (format == DataFormats.CommaSeparatedValue) { 
      if (lastCell && !inLastRow) 
       cell_suffix = Environment.NewLine; 
      else if (!lastCell) 
       cell_suffix = ","; 
     } else if (format == DataFormats.Html) { 
      if (inFirstRow && firstCell) 
       table_prefix = "<TABLE>"; 
      if (inLastRow && lastCell) 
       table_suffix = "</TABLE>"; 
      if (firstCell) 
       row_prefix = "<TR>"; 
      if (lastCell) 
       row_suffix = "</TR>"; 
      cell_prefix = "<TD>"; 
      cell_suffix = "</TD>"; 

      if (!Selected) { 
       value = "&nbsp;"; 
      } 
     } else { 
      return value; 
     } 

     value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix; 

     return value; 
    } 

제가 GetEditedFormattedValue을 무시하고 확인해야한다고 생각합니다 수 있습니다 :

니켈 수소, 기본 방법이 (http://www.eg.bucknell.edu/~cs208/subpages/software/src/mono-2.6.7/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCell.cs에서 모노에서 C# 코드)와 같은 무언가를 보인다 ClipboardContent 비트가 Context 인수에 설정되어있어 클립 보드 값을 가져 오는 지 여부를 감지 할 수 있으며, 그렇다면 DisplayValue가 아닌 ComboBox 셀 자손 값을 반환합니다.

GetEditedFormattedValue은 무시할 수 없습니다.

그래서 열에 플래그를 만들고, 셀의 GetClipboardContent이 실행중인 경우 플래그를 설정하고 플래그가 설정된 경우 표의 CellFormatting 이벤트에서 서식이 지정되지 않은 값을 반환합니다.

잘 작동합니다.

관련 문제