Results 1 to 6 of 6

Thread: DataGridView doesn't show added row

  1. #1
    robertb_NZ is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    91
    Reputation
    88

    DataGridView doesn't show added row

    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.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,813
    Reputation
    1707
    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.

  3. #3
    robertb_NZ is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    91
    Reputation
    88
    Thanks John, this gives me some useful clues. I need to figure out what's meant by
    Quote Originally Posted by jmcilhinney View Post
    ... 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.

  4. #4
    robertb_NZ is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    91
    Reputation
    88
    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

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,813
    Reputation
    1707
    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.

  6. #6
    robertb_NZ is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    91
    Reputation
    88
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •