473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Refreshing DataTable

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

Nov 21 '05 #1
1 1483
Simon,

I made this sample now. I don't set it on our site it is to simple for my
opinion. Can you have a look for yourself what that means accoording your
code.

At least there is no reason to set that datasource again in your code as you
do now.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = CreateTables()
dt.DefaultView.Sort = "State"
DataGrid1.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable = DirectCast(DataGrid1.DataSource, DataTable)
'The row above is to set the datatable not global not something
accoording your problem
dt.LoadDataRow(New Object() {"Michael Coles", "New Jersey"}, True)
End Sub
'To have a table to use is one created below
Private Function CreateTables() As DataTable
Dim dt As New DataTable("Persons")
dt.Columns.Add("Name", GetType(System.String))
dt.Columns.Add("State", GetType(System.String))
dt.LoadDataRow(New Object() {"Ken Tucker", "Florida"}, True)
dt.LoadDataRow(New Object() {"Cor Ligthert", "Holland"}, True)
Return dt
End Function
///
I hope this helps,

Cor
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Donnor | last post by:
Here's my question.. I can't seem to get my datagrid to refresh. I have a Dataset that automagically populates the datagrid. It's a two parameter SQL statement that I am using. Here's an...
31
by: Greg Scharlemann | last post by:
Given some recent success on a simple form validation (mainly due to the kind folks in this forum), I've tried to tackle something a bit more difficult. I'm pulling data down from a database and...
2
by: VM | last post by:
How can I display the contents of a datatable in a windows datagrid while the table is being filled? Here's my summarized code: delegate void loadAuditFileToTableDelegate(); private void...
1
by: VMI | last post by:
I added a datacolumn in my table that includes data from two other datacolumns joined together: ColJoin.Expression = "Col1 + ' ' + Col2"; When a user chooses a row through the Windows grid , I use...
1
by: MrNobody | last post by:
Simple question (I hope!): ater adding a new row to a DataTable, how do I get the DataGrid to refresh so it shows the new row? I didn't have to do anything special to see the result of...
3
by: steve | last post by:
Hi All How can I get the datagridview to redraw after changing the rowtemplate.height at runtime ( it is databound to a datatable) I have tried datagridview1.refresh etc to no avail (The row...
1
by: joechipubik | last post by:
I have a GridView in a FormView that has as its datasource a DataTable that is stored in the session cache. When I first load the page the GridView is displayed correctly, but on subsequent loads...
1
by: romerve | last post by:
Hello; i am having some problems trying to get a form that has a datagridview to refresh after a new record is created. I have a MDI container and menu form and add new record form; the menu...
3
by: veenna | last post by:
How can i refresh datagrid in vb.net. I want to display color in one cell in grid but when it loads first time it is working here is my code Public Class Dialog_Colorazione Const YEAR_START...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.