error when adding column to datatable: There is no row at position

G

Guest

Since upgrading to the 2.0 framework I am getting an error that was not
present in 1.1. I have a grid that is bound to a DataView, which was
initialized with a DataTable. I set up the initial rows & cols in the
DataTable, create the DataView, and bind the grid to it. After that, I try
to add a column to DataTable.Columns, and I get this error:

"There is no row at position 0."

at System.Data.DataView?.GetRecord?(Int32 recordIndex)
at System.Data.DataView?.GetRow?(Int32 index)
at System.Data.DataView?.UpdateDataRowViewCache?()
at System.Data.DataView?.OnListChanged?(ListChangedEventArgs? e)
at System.Data.DataView?.ColumnCollectionChanged?(Object sender,
CollectionChangeEventArgs? e)
at System.Data.DataViewListener?.ColumnCollectionChanged?(Object sender,
CollectionChangeEventArgs? e)
at
System.Data.DataColumnCollection?.OnCollectionChanged?(CollectionChangeEventArgs? ccevent)
at System.Data.DataColumnCollection?.AddAt?(Int32 index, DataColumn?
column)
at System.Data.DataColumnCollection?.Add(DataColumn? column)

I can't fathom why the code for adding a column is trying to update the
_row_ cache of the DataView. Also it doesn't seem to matter that there
actually is a row at position 0...

As a workaround I am setting the DataView's DataTable to Nothing, adding the
column, then setting the DataView's DataTable back to the DataTable.

But if someone could explain why this is happening in 2.0 I would appreciate
it.

Thanks.
 
C

Cor Ligthert [MVP]

SVBridget,

Probably there is something wrong in your code, but that we cannot see.
Mostly it helps if we can see that as well, because you cannot find the
reason even with that code.

Therefore maybe it will help if you show the relevant parts of that

Cor
 
G

Guest

Sure, here's the repro code. Also as I noted previously this did not throw
an error in 1.1.

I am including the form code and the designer code below. Click Setup, then
click Error, and the error will appear.

I currently have two workarounds: a) set the dataview's datasource to
nothing, add the column, then set it back to datatable b) take out
beginloaddata and endloaddata.

-B

-------------------- BEGIN FORM CODE ------------------------
Imports System.Data

Public Class Form1

Private Sub cmdSetup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSetup.Click
Dim table As DataTable = New DataTable("Flowchart")

Dim myDataColumn As DataColumn
myDataColumn = New DataColumn
myDataColumn.DataType = GetType(System.String)
myDataColumn.ColumnName = "Flowchart"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "Flowchart"
myDataColumn.Unique = False
table.Columns.Add(myDataColumn)

' Hidden column for object type
myDataColumn = New DataColumn
myDataColumn.DataType = GetType(System.Double)
myDataColumn.ColumnName = "ObjectType"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ObjectType"
myDataColumn.Unique = False
table.Columns.Add(myDataColumn)

' hidden column for sort string
myDataColumn = New DataColumn
myDataColumn.DataType = GetType(System.String)
myDataColumn.ColumnName = "Sort"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "Sort"
myDataColumn.Unique = False
table.Columns.Add(myDataColumn)

Dim dataview As New DataView(table)
dataview.Sort = "ObjectType, Sort"
grdFlowChartOrig.DataSource = dataview

AddFlowChartRow(2, "A")
AddFlowChartRow(3, "B")
AddFlowChartRow(1, "C")
AddFlowChartRow(4, "D")

AddFlowChartColumn(table)
AddFlowChartColumn(table)
AddFlowChartColumn(table)

AddFlowChartRow(2, "A")
AddFlowChartRow(3, "B")
AddFlowChartRow(1, "C")
AddFlowChartRow(4, "D")

grdFlowChartOrig.Refresh()

End Sub


Private Sub cmdError_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdError.Click
Dim objTable As DataTable = DirectCast(grdFlowChartOrig.DataSource,
DataView).Table
Dim objView As DataView = DirectCast(grdFlowChartOrig.DataSource,
DataView)

objTable.Rows.Clear()
objTable.BeginLoadData()
While objTable.Columns.Count > 3
objTable.Columns.RemoveAt(objTable.Columns.Count - 1)
End While

AddFlowChartRow(3, "E")
AddFlowChartRow(1, "E")
AddFlowChartRow(2, "E")

AddFlowChartColumn(objTable)
objTable.EndLoadData()
objTable.AcceptChanges()
End Sub


Public Sub AddFlowChartRow(ByVal nObjectType As Double, Optional ByVal
strSort As String = "")
Static i As Integer = 0

Dim objTable As DataTable = DirectCast(grdFlowChartOrig.DataSource,
DataView).Table
Dim objRow As DataRow = objTable.NewRow()
objRow(0) = "row" & CStr(i)
objRow(1) = nObjectType
objRow(2) = strSort
objTable.Rows.Add(objRow)
i = i + 1
End Sub

Public Sub AddFlowChartColumn(ByVal objTable As DataTable)
Static i As Integer = 0

Dim myDataColumn As New DataColumn
myDataColumn.DataType = GetType(System.String)
myDataColumn.ColumnName = "new column" & CStr(i)
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "new column" & CStr(i)
myDataColumn.Unique = False
objTable.Columns.Add(myDataColumn) ' ERROR THROWN HERE

i = i + 1
End Sub


End Class

-------------------- END FORM CODE ---------------------------



-------------------- BEGIN DESIGNER CODE --------------------
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.grdFlowChartOrig = New System.Windows.Forms.DataGridView
Me.cmdSetup = New System.Windows.Forms.Button
Me.cmdError = New System.Windows.Forms.Button
CType(Me.grdFlowChartOrig,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'grdFlowChartOrig
'
Me.grdFlowChartOrig.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.grdFlowChartOrig.Location = New System.Drawing.Point(12, 35)
Me.grdFlowChartOrig.Name = "grdFlowChartOrig"
Me.grdFlowChartOrig.Size = New System.Drawing.Size(423, 169)
Me.grdFlowChartOrig.TabIndex = 0
'
'cmdSetup
'
Me.cmdSetup.Location = New System.Drawing.Point(454, 49)
Me.cmdSetup.Name = "cmdSetup"
Me.cmdSetup.Size = New System.Drawing.Size(100, 58)
Me.cmdSetup.TabIndex = 1
Me.cmdSetup.Text = "Setup"
Me.cmdSetup.UseVisualStyleBackColor = True
'
'cmdError
'
Me.cmdError.Location = New System.Drawing.Point(457, 129)
Me.cmdError.Name = "cmdError"
Me.cmdError.Size = New System.Drawing.Size(97, 58)
Me.cmdError.TabIndex = 2
Me.cmdError.Text = "Trigger Error"
Me.cmdError.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(580, 253)
Me.Controls.Add(Me.cmdError)
Me.Controls.Add(Me.cmdSetup)
Me.Controls.Add(Me.grdFlowChartOrig)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.grdFlowChartOrig,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
Friend WithEvents grdFlowChartOrig As System.Windows.Forms.DataGridView
Friend WithEvents cmdSetup As System.Windows.Forms.Button
Friend WithEvents cmdError As System.Windows.Forms.Button

End Class
------------------------- END DESIGNER CODE ---------------------------------
 
G

Guest

Thanks for looking at this, Cor, I appreciate it. I couldn't find a good
spot to put this on the connect.microsoft.com site (maybe you have access to
something that I don't have) so I called MS support and filed a bug. I will
post whatever MS responds with here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top