RedditPP Mascot

RedditPP

r/visualbasic • … • u/mgerics

Cannot add row to a datatable if it has been set as datasource for a datagridview and the datagridview has been soreted.

I programtically add columns to a datatable, populate it with data, then set it as as datasource for a datagridview.

I can repopulate with the same/simlar data multiple times UNLESS I click a column header on the datagridview to resort it.

When I then attempt to repopulate the datatable with the same or similar data, I get "object-reference-not-set-to-an-instance-of-an-object" when attempting to add a row to the table. (dim arow as datarow = dr.newrow)

The original routine to fill the datatable first cleared the rows (dt.rows.clear) and worked fine provided I didn't sort on the datagridview as stated.

Also tried setting datagridview.datasource = nothing, still the same.

Finally did dt = new datatable, copied the code that created the columns, proceeded to populate the table, and all works fine.

The datatable has no indices, keys, etc., is just a collection of string columns with one integer column.

It works, but I can't figure out what is going on.

Anyone know why this is happening ?

— 0