DataGridView doesn't show added row

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
My program assigns a datagridview.Datasource with
dgvLayouts.DataSource = Layoutnames​

Layoutnames is defined
Property Layoutnames As New List(Of LayoutElement)​

In my test there are initially 4 members of the list, and dgvLayouts is displayed as expected with 4 rows, each with the fields of class LayoutElement. My program can manipulate the data in LayoutNames, for example swapping data in two rows, and this was reflected in the datagridview once I'd learned to use
dgvLayouts.Refresh()​

However sometimes my program wants to add a new line to the grid. Logic for this case is: -
If new line wanted Then
LE = new LayoutElement
Assign values to LE
Layoutnames.add(LE)
dgvlayouts.refresh​

Although the debugger shows me that there are now 5 members of LayoutNames, the datagridview continues to display only 4 rows. How can I make it display the correct number?

I have experimented with
dgvlayouts.rows.add(...)​
but this throws an error ("rows can't be programmatically added ... when the control is data-bound").
In Visual Studio dgvlayouts has default property AllowUserToAddRows=True

I've looked through the properties of dgvlayouts, but nothing leaps out at me as possibly wrong, or worth experimenting with.

Can anybody help?

Thank you, Robert.
 
The problem is that, in order to refresh its display, the grid has to be notified that it needs to and a List(Of T) doesn't provide any such notification. What you will need to do is bind your List to a BindingSource and then bind that to the grid. When you update the List in code, you can call the appropriate method of the BindingSource so that it raises an event to notify the grid to refresh. You'll need to call ResetCurrentItem, ResetItem or ResetBindings, depending on the nature of the change you made.
 
Thanks John, this gives me some useful clues. I need to figure out what's meant by
... bind your List to a BindingSource and then bind that to the grid. ... ResetCurrentItem, ResetItem or ResetBindings, depending on the nature of the change you made.

So I've got some reading to do, but at least I now know what to look up. I don't suppose that you can show me some sample code :)

Regards, Robert.
 
Problem fixed! I changed
Property Layoutnames As New List(Of LayoutElement)​
to
Property Layoutnames As New BindingList(Of LayoutElement)​
and everything worked perfectly! First time! No other changes!

Thank you John
 
Like the BindingSource, the BindingList(Of T) class implements the IBindingList interface, which is where the events come from that the DataGridView needs to automatically refresh. Given that a BindingList is intended specifically for binding, it probably shouldn't be used unless your list is specific to the presentation layer. If the list is specific to the presentation layer then a BindingList is quite appropriate.
 
Thanks for the extra information. In this case
Property Layoutnames As New BindingList(Of LayoutElement)​
is a property of class AlignLayouts which owns dgvLayouts (the datagridview), so I guess it's OK.

Regards, Robert
 
Back
Top