VB2010 I have created a DataTable manually so it does not come from a database. I have assigned it to a combobox and it displays my column of data. If I change the DataTable do I have to re-establish the link?
'assign first table
dt = GetFirstTable()
cbo.DataSource = dt
cbo.DisplayMember = "Time"
cbo.ValueMember = "Time"
'print out items in combobox
'assign second table
dt = GetSecondTable()
'cbo.DataSource = dt 'do i have to re-connect here?
'print out items in combobox
It seems if I do not re-establish the link I get the same items. I though since the cbo was already linked to the dt variable i didn’t need to re-link it each time. Is that how that works or am I doing something wrong here?
When you assign
cbo.DataSource = dt, and you then recreatedt,cbo.DataSourcewill keep pointing to the old table. This is pure pointer logic working here, same principle applies to all .NET code. It does not mean anything that you are re-using the same variable. You could have instead createddt2and used that, behavior would be the same. So yes, if you recreate theDataTable, you need to reassignDataSourceagain. However, if you change the originaldt, i.e. add rows, those will appear, so you will not need to reassignDataSource. Here is a code sample, to illustrate the approach: