Refreshing DataTable

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi All,

I have a DataGrid bound to a DataTable. Every once in a while I need
to refresh the data in the table so that the updated data is displayed
in the DataGrid. Here's how I am attempting this:

'clear datatable
datatbl.Rows.Clear()

If PLCCount() <> 0 Then
'add rows to datatable containing PLC info
For p = 0 To PLCCount() - 1
Dim rowinfo As DataRow
rowinfo = datatbl.NewRow()
rowinfo("ID") = plcID(p)
rowinfo("Road") = PLCRoadName(p)
rowinfo("Start Time") = MyDate(plcStartTime(p))
rowinfo("End Time") = MyDate(plcEndTime(p))
rowinfo("Location") = plcLocationDescription(p)
rowinfo("Cause") = plcCause(p)
'add info to datatable
Try
'out-of-bounds error here
datatbl.Rows.Add(rowinfo)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next p
'bind to datatable to datagrid
grdClosures.DataSource = datatbl
End If

I clear the rows in the DataTable and re-populate. However, when I try
and add the new rows i get an out-of-bounds exception in the Add
function.

This has something to do with the fact that I clear the rows and then
create new ones but I don't know nor understand why this is a problem.

Can anyone help?

Many thanks

Simon

.


Quantcast