“没有行可以添加到没有列的 DataGridView 控件中。必须先添加列。”向 DataTable 添加行时
“No row can be added to a DataGridView control that does not have columns. Columns must be added first.” when adding rows to DataTable
我在尝试向 DataTable 添加行时收到以下消息:“不能向没有列的 DataGridView 控件添加任何行。必须先添加列。”有趣的是,我试图将行添加到 的 DataTable 在添加行时 确实有三列。似乎故障发生在货币管理器上,位于行插入堆栈的下方。这是我的代码:
Using DT As DataTable = Me.Tables(TableNamePrefix & " " & CStr(Index + 1))
DT.Rows.Clear()
DT.Columns.Clear()
For Each LF As cLoadField In LFs
LF.UseDefaultUnits(DispOpt.Units, DispOpt.Format)
DT.Columns.Add(LF.Name(Cond)).Caption = LF.Name(Cond)
If Not LF.UnitString(DispOpt) = "" Then
DT.Columns(LF.Name(Cond)).Caption = LF.Name(Cond) & vbCrLf & LF.UnitString(DispOpt)
End If
If LF.FieldType = hecLoadFields.hecLF_LengthPaidOut Then LPO_Name = LF.Name(Cond)
Next
For Each HW As CHawser In Hawsers
If Not Cond.Loads.NameExists(HW.Name) Then Continue For
DR = DT.NewRow
For Each LF As cLoadField In LFs
LF.TankName = HW.Name
DR(LF.Name(Cond)) = LF.Value(Cond)
Next
DT.Rows.Add(DR)
Next
End Using
这是调用堆栈:
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewRowCollection.InsertInternal(int rowIndex, System.Windows.Forms.DataGridViewRow dataGridViewRow, bool force) + 0x342 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x5ed bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x2b bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x15 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x53e bytes
System.Data.dll!System.Data.DataView.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x136 bytes
System.Data.dll!System.Data.DataView.IndexListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x1c bytes
System.Data.dll!System.Data.DataView.IndexListChangedInternal(System.ComponentModel.ListChangedEventArgs e) + 0x47 bytes
System.Data.dll!System.Data.DataViewListener.IndexListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x3b bytes
System.Data.dll!System.Data.Index.OnListChanged.AnonymousMethod__2(System.Data.DataViewListener listener, System.ComponentModel.ListChangedEventArgs args, bool arg2, bool arg3) + 0x7 bytes
System.Data.dll!System.Data.Listeners<System.__Canon>.Notify<System.__Canon,bool,bool>(System.__Canon arg1, bool arg2, bool arg3, System.Data.Listeners<System.__Canon>.Action<System.__Canon,System.__Canon,bool,bool> action) + 0x90 bytes
System.Data.dll!System.Data.Index.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0xa4 bytes
System.Data.dll!System.Data.Index.OnListChanged(System.ComponentModel.ListChangedType changedType, int index) + 0x38 bytes
System.Data.dll!System.Data.Index.InsertRecord(int record, bool fireEvent) + 0xdf bytes
System.Data.dll!System.Data.Index.ApplyChangeAction(int record, int action, int changeRecord) + 0x34 bytes
System.Data.dll!System.Data.Index.RecordStateChanged(int record, System.Data.DataViewRowState oldState, System.Data.DataViewRowState newState) + 0x40 bytes
System.Data.dll!System.Data.DataTable.RecordStateChanged(int record1, System.Data.DataViewRowState oldState1, System.Data.DataViewRowState newState1, int record2, System.Data.DataViewRowState oldState2, System.Data.DataViewRowState newState2) + 0xaf bytes
System.Data.dll!System.Data.DataTable.SetNewRecordWorker(System.Data.DataRow row, int proposedRecord, System.Data.DataRowAction action, bool isInMerge, bool suppressEnsurePropertyChanged, int position, bool fireEvent, out System.Exception deferredException) + 0x540 bytes
System.Data.dll!System.Data.DataTable.InsertRow(System.Data.DataRow row, long proposedID, int pos, bool fireEvent) + 0xf5 bytes
System.Data.dll!System.Data.DataRowCollection.Add(System.Data.DataRow row) + 0x1a bytes
如有任何帮助,我们将不胜感激!
此 DataTable 是位于我的应用程序主窗体上的 DataGridView 的数据源。原来是我没有设置DataGridView的AutoGenerateColumns属性,这是这个错误的根源。
我在尝试向 DataTable 添加行时收到以下消息:“不能向没有列的 DataGridView 控件添加任何行。必须先添加列。”有趣的是,我试图将行添加到 的 DataTable 在添加行时 确实有三列。似乎故障发生在货币管理器上,位于行插入堆栈的下方。这是我的代码:
Using DT As DataTable = Me.Tables(TableNamePrefix & " " & CStr(Index + 1))
DT.Rows.Clear()
DT.Columns.Clear()
For Each LF As cLoadField In LFs
LF.UseDefaultUnits(DispOpt.Units, DispOpt.Format)
DT.Columns.Add(LF.Name(Cond)).Caption = LF.Name(Cond)
If Not LF.UnitString(DispOpt) = "" Then
DT.Columns(LF.Name(Cond)).Caption = LF.Name(Cond) & vbCrLf & LF.UnitString(DispOpt)
End If
If LF.FieldType = hecLoadFields.hecLF_LengthPaidOut Then LPO_Name = LF.Name(Cond)
Next
For Each HW As CHawser In Hawsers
If Not Cond.Loads.NameExists(HW.Name) Then Continue For
DR = DT.NewRow
For Each LF As cLoadField In LFs
LF.TankName = HW.Name
DR(LF.Name(Cond)) = LF.Value(Cond)
Next
DT.Rows.Add(DR)
Next
End Using
这是调用堆栈:
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewRowCollection.InsertInternal(int rowIndex, System.Windows.Forms.DataGridViewRow dataGridViewRow, bool force) + 0x342 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x5ed bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x2b bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x15 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x53e bytes
System.Data.dll!System.Data.DataView.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x136 bytes
System.Data.dll!System.Data.DataView.IndexListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x1c bytes
System.Data.dll!System.Data.DataView.IndexListChangedInternal(System.ComponentModel.ListChangedEventArgs e) + 0x47 bytes
System.Data.dll!System.Data.DataViewListener.IndexListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x3b bytes
System.Data.dll!System.Data.Index.OnListChanged.AnonymousMethod__2(System.Data.DataViewListener listener, System.ComponentModel.ListChangedEventArgs args, bool arg2, bool arg3) + 0x7 bytes
System.Data.dll!System.Data.Listeners<System.__Canon>.Notify<System.__Canon,bool,bool>(System.__Canon arg1, bool arg2, bool arg3, System.Data.Listeners<System.__Canon>.Action<System.__Canon,System.__Canon,bool,bool> action) + 0x90 bytes
System.Data.dll!System.Data.Index.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0xa4 bytes
System.Data.dll!System.Data.Index.OnListChanged(System.ComponentModel.ListChangedType changedType, int index) + 0x38 bytes
System.Data.dll!System.Data.Index.InsertRecord(int record, bool fireEvent) + 0xdf bytes
System.Data.dll!System.Data.Index.ApplyChangeAction(int record, int action, int changeRecord) + 0x34 bytes
System.Data.dll!System.Data.Index.RecordStateChanged(int record, System.Data.DataViewRowState oldState, System.Data.DataViewRowState newState) + 0x40 bytes
System.Data.dll!System.Data.DataTable.RecordStateChanged(int record1, System.Data.DataViewRowState oldState1, System.Data.DataViewRowState newState1, int record2, System.Data.DataViewRowState oldState2, System.Data.DataViewRowState newState2) + 0xaf bytes
System.Data.dll!System.Data.DataTable.SetNewRecordWorker(System.Data.DataRow row, int proposedRecord, System.Data.DataRowAction action, bool isInMerge, bool suppressEnsurePropertyChanged, int position, bool fireEvent, out System.Exception deferredException) + 0x540 bytes
System.Data.dll!System.Data.DataTable.InsertRow(System.Data.DataRow row, long proposedID, int pos, bool fireEvent) + 0xf5 bytes
System.Data.dll!System.Data.DataRowCollection.Add(System.Data.DataRow row) + 0x1a bytes
如有任何帮助,我们将不胜感激!
此 DataTable 是位于我的应用程序主窗体上的 DataGridView 的数据源。原来是我没有设置DataGridView的AutoGenerateColumns属性,这是这个错误的根源。