ArgumentOutOfRange在datagridview中分配数据源时发生异常。
编辑: 当我使用List代替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 :(得分:0)
我找到了问题的原因。
在我的DataGridView AutoSizeColumnsMode
模式下设置为AllCell
我将其更改为None
。然后删除此错误。
在单声道框架中,列大小计算似乎存在内部问题,我认为这是单声道框架的错误。但是目前我的问题已经解决了。