2014-10-17 56 views
0

ArgumentOutOfRange在datagridview中分配数据源时发生异常。ArgumentOutOfRange指定单声道datagridview的数据源时出现异常

编辑: 此代码工作正常,当我使用列表代替BindingList。

环境: 单声道4.0.12。 C#WinForm应用程序

代码:

public class DataGridViewStudyReference : CustomControls.DataGridViewDataEntry<StudyRefrenceValueBusinessObjectNew> 
{ 
} 

public class DataGridViewDataEntry<BindingSourceType> : DataGridView 
{ 
     public void ResetDataSource() 
     { 
      if (DataEntrySource == null) 
      { 
       DataEntrySource = new BindingList<BindingSourceType>(); 
       this.DataSource = DataEntrySource; 
      } 
      else 
      { 
       DataEntrySource.Clear(); 
      } 
     } 
} 

public class StudyRefrenceValueBusinessObjectNew 
{ 
    public long StudyReferenceID { get; set; } 

    public long StudyID { get; set; } 

    public int GenderValue { get; set; } 

    public int AgeInitial { get; set; } 

    public int AgeFinal { get; set; } 

    public string TextValue { get; set; } 

    public int ValueInitial { get; set; } 

    public int ValueFinal { get; set; } 

} 

满异常详细信息是如下

消息:

索引小于0或大于或等于列表计数。 参数名称:索引1

堆栈跟踪:

at System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) [0x00000] in <filename unknown>:0 
    at System.Collections.ArrayList.get_Item (Int32 index) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridViewCellCollection.get_Item (Int32 index) [0x00000] in <filename unknown>:0 
    at (wrapper remoting-invoke-with-check) System.Windows.Forms.DataGridViewCellCollection:get_Item (int) 
    at System.Windows.Forms.DataGridView.CalculateColumnCellWidth (Int32 index, DataGridViewAutoSizeColumnMode mode) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.AutoResizeColumnInternal (Int32 columnIndex, DataGridViewAutoSizeColumnMode mode) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.AutoResizeColumnsInternal() [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.OnColumnAddedInternal (System.Windows.Forms.DataGridViewColumnEventArgs e) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.OnColumnCollectionChanged (System.Object sender, System.ComponentModel.CollectionChangeEventArgs e) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridViewColumnCollection.OnCollectionChanged (System.ComponentModel.CollectionChangeEventArgs e) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridViewColumnCollection.Add (System.Windows.Forms.DataGridViewColumn dataGridViewColumn) [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.DoBinding() [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.ReBind() [0x00000] in <filename unknown>:0 
    at System.Windows.Forms.DataGridView.set_DataSource (System.Object value) [0x00000] in <filename unknown>:0 
    at LabManagement.WinForm.CustomControls.DataGridViewDataEntry`1[LabManagement.Model.Study.StudyRefrenceValueBusinessObjectNew].ResetDataSource() [0x00020] in /home/alit/Projects/LabManagementSolution/LabManagement/LabManagement.WinForm/CustomControls/DataGridViewDataEntry.cs:64 

任何人可以帮助我,为什么这个异常发生的?

回答

0

我得到了问题的原因。

在我的DataGridView AutoSizeColumnsMode模式设置为AllCell我改变它为None。然后删除此错误。 单声道框架中的列大小计算似乎存在内部问题,我认为这是单声道框架的错误。但目前我的问题已解决。

相关问题