DataGridView bound to datasource requires data in datasource?

I have a DataGridView which I want to use to add dynamic data to it. To start with my datasource a SortedList has no data in it. I will dyamically add data to it over time. However every time I add a new row of data

privatevoid addSecurityToolStripMenuItem_Click(object sender,EventArgs e)

{

string symbol ="Foo" + newSecurityID;

newSecurityID++;

Security sec =newSecurity(symbol, 100.12, 100.15, 100, 200,SecurityTypeEnum.eStock,true,"Up");

sortedList.Add(symbol, sec);

bindingSource1.Add(sec);

dataGridView1.Invalidate();

}

the row is blank. I have used the debugger to see that the data is there.

Then a strange thing happened, I added some data to the datasource prior to the ...

bindingSource1.DataSource = sortedList.Values;

...And data started to showup. It looks like if I add just one row of data prior to doing the binding, any rows added after that point will show up. But I must have one added before the bind or else all the dynamically added rows will be blank.

I tried out other samples that I have created. In one case removing the data for the datasource causes a crash with the error "Object reference not set to an instance of an object" somewhere deep in windows code.

Is this a known bug?

[2111 byte] By [JeffM123] at [2008-1-3]
# 1

Hi JeffM123,

Use The ResetBindings method of BindingSource when you add or remove any item from the list.The ResetBindings method informs all controls bound to the BindingSource to refresh their values. The method does this by raising the ListChanged event at least once. ResetBindings is automatically called whenever another member makes major changes to the data-binding, such as setting the DataSource or DataMember properties. However, the programmer can also call this method explicitly.

Hope it helps.

Regards

Rong-ChunZhang-MSFT at 2007-9-25 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...