2009-07-23 3 views
2

DataGrid가 많은 응용 프로그램에서는 크기 조정에 사용되는 행 사이의 공간을 두 번 클릭 할 때 예외가 발생하기를 좋아합니다. 예외는 The " DataGridColumnStyle cannot be used because it is not associated with a Property or a Column in the DataSource.DataGrid에서 행 사이를 두 번 클릭하여 발생하는 예외는 어떻게 처리합니까?

입니다. 대부분의 DataGrid 기반 폼은 GridForm이라는 한 폼에서 상속됩니다. 우리의 DataSource는 DataView입니다. 더블 클릭 이벤트 핸들러에 중단 점을 설정할 수는 있지만 결코 도달하지 못했습니다. except는 컨트롤을 호스팅하는 전체 양식의 Show/ShowDialog 호출에서 catch됩니다. 현재 .NET 3.5를 실행 중이지만이 기능의 대부분은 .NET 1.1에 구축되었습니다. 당시에도 같은 문제가있었습니다. 그래서이 문제를 해결하는 것은 놀라운 것, http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=5780

이것은 우리가 좋은 3-4년 주변에 잠복했던 한 버그가 : StackOverflow의의 자신의 조엘 Coehoorn 보인다

여기에 같은 문제로 실행 한합니다.


다음은 예외입니다. 불만을 제기하는 DataGridColumnStyle은 애플리케이션의 각 그리드마다 다릅니다. 필자는 이것이 바운드 데이터 집합에없는 열을 표시하는 열 스타일을 가지고 있음을 의미합니다. 데이터 세트에 포함 된 열은 주어진 양식의 필요성에 따라 달라 지므로 실제로 존재할 수 있거나 존재하지 않을 수있는 열에 대해 몇 가지 스타일을 정의해야합니다.
System.InvalidOperationException: DataGridColumnStyle of 'Real-Time Bill' cannot be used because it is not associated with a Property or Column in the DataSource. 
    at System.Windows.Forms.DataGridColumnStyle.CheckValidDataSource(CurrencyManager value) 
    at System.Windows.Forms.DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager source, Int32 rowNum) 
    at System.Windows.Forms.DataGridBoolColumn.GetColumnValueAtRow(CurrencyManager lm, Int32 row) 
    at System.Windows.Forms.DataGrid.RowAutoResize(Int32 row) 
    at System.Windows.Forms.DataGrid.OnMouseDown(MouseEventArgs e) 
    at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 
    at CLS.Presentation.MainForm.Main(String[] args) in C:\Users\stuartb.CLS\Documents\Projects\Genesis\CLS.Presentation\MainForm.cs:line 2712 
요청으로


, 여기에 문제의 컬럼에 대한 정의입니다.
예외적으로이 컬럼이 항상 그런 것은 아닙니다. 이전 편집에서 언급했듯이 바인딩 된 DataTable에없는 열을 찾는 열 스타일로 인해이 문제가 발생합니다. 이 예제에서 ConsumptionBill은 바운드 DataTable에서 이 아니고입니다. 이 경우의 동작은 열을 표시하지 않고 단순히 행 테두리 두 번 클릭시 충돌 및 레코딩하는 것입니다.

this.dataGridBoolColumnClientsConsumptionBill.Alignment = System.Windows.Forms.HorizontalAlignment.Center; 
this.dataGridBoolColumnClientsConsumptionBill.AllowNull = false; 
this.dataGridBoolColumnClientsConsumptionBill.HeaderText = "Real-Time Bill"; 
this.dataGridBoolColumnClientsConsumptionBill.MappingName = "ConsumptionBill"; 
this.dataGridBoolColumnClientsConsumptionBill.Width = 75; 

답변

3

John의 도움을 받아 약간의 가이드 파고 덕분에 일반적으로 다음 방법으로 문제를 해결할 수있었습니다.

IList<DataGridColumnStyle> missingColumnStyles = new List<DataGridColumnStyle>(); 
/// <summary> 
/// Adjust the grid's definition to accept the given DataTable. 
/// </summary> 
/// <param name="table"></param> 
protected virtual void AdjustGridForData(DataTable table, DataGrid grid) 
{ 
    // Remove column styles whose mapped columns are missing. 
    // This fixes the notorious, uncatchable exception caused when double-clicking row borders. 
    var columnStyles = grid.TableStyles[table.TableName].GridColumnStyles; 

    // Add previously removed column styles back to the grid, in case the new bound table 
    // has something we removed last time this method was executed. 
    foreach (var missingColumnStyle in missingColumnStyles) 
     columnStyles.Add(missingColumnStyle); 
    missingColumnStyles.Clear(); 

    // Move the offending column styles into a separate list. 
    var missingColumns = new List<string>(); 
    foreach (DataGridColumnStyle style in columnStyles) 
    { 
     if (!table.Columns.Contains(style.MappingName)) 
      missingColumns.Add(style.MappingName); 
    } 
    foreach (var column in missingColumns) 
    { 
     missingColumnStyles.Add(columnStyles[column]); 
     columnStyles.Remove(columnStyles[column]); 
    } 
} 
관련 문제