locked
How do add rows to a databound datagridview grid RRS feed

  • Question

  • Dumb question I know given what I want is so basic, but how do I add and delete rows from a databound dataggridview grid. The code below displays a grid with no apparent ability to add rows. Also, when I add a row to the databound List it is not displayed.



    • Edited by trogan Saturday, August 22, 2015 4:05 AM
    Saturday, August 22, 2015 3:51 AM

Answers

  • Hello,

    Here is a working demo project and the code is shown below.

    Public Class Class1
        Public Property FirstName As String
        Public Property LastName As String
        Public Sub New()
        End Sub
    End Class
    

    ..

    Public Class Form1
        WithEvents bsData As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsData.DataSource = New List(Of Class1) From
                {
                    New Class1 With
                        {
                            .FirstName = "Karen",
                            .LastName = "Payne"
                        },
                    New Class1 With
                        {
                            .FirstName = "Anne",
                            .LastName = "Jone"
                        }
            }
    
            DataGridView1.DataSource = bsData
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CType(bsData.DataSource, List(Of Class1)).Add(
                New Class1 With
                {
                    .FirstName = "Bill",
                    .LastName = "Smith"
                }
            )
    
            bsData.ResetBindings(False)
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If bsData.Current IsNot Nothing Then
                bsData.RemoveCurrent()
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    • Proposed as answer by Frank L. Smith Saturday, August 22, 2015 2:23 PM
    • Marked as answer by trogan Saturday, August 22, 2015 10:49 PM
    Saturday, August 22, 2015 2:08 PM

All replies

  • Dumb question I know given what I want is so basic, but how do I add and delete rows from a databound dataggridview grid. The code below displays a grid with no apparent ability to add rows. Also, when I add a row to the databound List it is not displayed.

    Adding to the data source is the correct way to do it.  You have posted an image instead of code, so it is difficult to see what the problem might be.  Are you quite sure that the item being added is displayable?

    Saturday, August 22, 2015 4:50 AM
  • What? You add a new Class1 to the List(of Class1) like you are already doing  with some data in the class and you rebind the List(of Class1) again to the datasource.

    The code for add......

    add new class to the List(of Class1)

    rebind the List(of T) with the new Class1 to the datasource again

    Saturday, August 22, 2015 4:56 AM
  • Thx DA924. And can you pls tell me the code to rebind the grid?
    Saturday, August 22, 2015 5:45 AM
  • Thx Acamar. The source is in the image. Apparently I need to rebind the grid after add to the List. Horribly disappointing that I have to add (it would appear) Add and Delete buttons.
    Saturday, August 22, 2015 5:48 AM
  • The code below seems to do the trick. Had to search forums for a long time before coming across this.

    Public Class Form1

        Public l As List(Of Class1) = New List(Of Class1)
        Public bindingSource As BindingSource

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            l.Add(New Class1("ABC", "DEF"))
            l.Add(New Class1("HIJ", "KLM"))
            DataGridView1.AllowUserToAddRows = True
            DataGridView1.AllowUserToDeleteRows = True


            BindingSource = New BindingSource()
            bindingSource.DataSource = l
            DataGridView1.DataSource = bindingSource

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            l.Add(New Class1("bbvbv", "gfgfgfg"))
            bindingSource.ResetBindings(False)


        End Sub

    End Class

    Saturday, August 22, 2015 5:58 AM
  • Thx Acamar. The source is in the image. Apparently I need to rebind the grid after add to the List. Horribly disappointing that I have to add (it would appear) Add and Delete buttons.

    Images aren't visible to everyone in the forums, so the code is just a guess.

    I don't understand the reference to Add and Delete buttons, but if you show the code used to add to the data source the problem may be apparent.

    Saturday, August 22, 2015 6:03 AM
  • The code below seems to do the trick. Had to search forums for a long time before coming across this.

    Searching the forums is usually not required - the description attached to the object is the best place to start.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings(v=vs.110).aspx

    Saturday, August 22, 2015 6:06 AM
  • somemethod() -- that you called

    dim cls1 = new Class1()

    cls1.Name = "test"

    cls1.Street = "Some Street"

    1.Add(cls1)

    DG.Datasource = 1

    end sub

    https://msdn.microsoft.com/en-us/library/dd293589.aspx?f=255&MSPPError=-2147217396

    public class Class1

    public Property Name as String

    public Property Street as String

    end class

    https://msdn.microsoft.com/en-us/library/bb531244.aspx

    OR

    in the Method()

    1.Add(new Class1 with {.Name = "Test", .Street = "Some street"})

    DG.Source = 1

    Saturday, August 22, 2015 6:09 AM
  • Acamar, the image showed that you could not add or delete rows from the grid using the code below and also that a row added to the bound control did not display. This is a major, major bug that has vast numbers of people on vb forums seeking answers.

    If you cant view images (cant imagine why not), you may be wasting peoples time.

    MSDN Forums are more often than not useless because theydon't provide code or what is provided is second-rate. Foe example  https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings(v=vs.110).asp

    Public Class Form1

        Public MyList As List(Of Class1) = New List(Of Class1)

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            MyList.Add(New Class1("ABC", "DEF"))
            MyList.Add(New Class1("HIJ", "KLM"))
            DataGridView1.AllowUserToAddRows = True
            DataGridView1.AllowUserToDeleteRows = True
            DataGridView1.DataSource = MyList

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            MyList.Add(New Class1("bbvbv", "gfgfgfg"))
            DataGridView1.Refresh()

        End Sub

    End Class

    Public Class Class1

        Property p1 As String
        Property P2 As String

        Public Sub New(s1 As String, s2 As String)
            p1 = s1
            P2 = s2
        End Sub

    End Class


    • Edited by trogan Saturday, August 22, 2015 9:38 AM
    Saturday, August 22, 2015 9:09 AM
  • If you cant view images (cant imagine why not), you may be wasting peoples time.

    MSDN Forums are more often than not useless because theydon't provide code or what is provided is second-rate. Foe example  https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings(v=vs.110).asp

    Viewing the image is not the issue.  Code in an image cannot be cut and pasted into an application.  If you want it tested (and that's by far the best way to get an answer to your problem) you are asking people to type it in from scratch, and that's likely to make many people just move onto something simpler.   There is a reason that points are awarded for including a code snippet in the post.

    The comment about MSDN Forums is accurate, as the forums are difficult to search and often include code that is incorrect or not relevant.  But the example you quote is MSDN, which is by far the best place to search.  I can't see what is wrong or confusing about the description at that site, but if you think it can be improved there is a facility for providing feedback at the bottom, which might be noticed by people at Microsoft, unlike the forums.

    Saturday, August 22, 2015 10:06 AM
  • OK, thx, I will provide code in future (onenote OCR on the image gives a pretty good result) Below is code provided on MSDN for https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings(v=vs.110).asp.

    Not satisfactory and, unfortunately, pretty typical of MSDN.

    The following code example uses a BindingSource component to bind an array list, which does not provide change notification. An item is removed from the list, and the bound controls are notified of the change by calling the ResetBindings method. This code example is part of a larger example provided in How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource.

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click
    
        ' If items remain in the list, remove the first item.  
        If states.Count > 0 Then
            states.RemoveAt(0)
    
            ' Call ResetBindings to update the textboxes.
            bindingSource1.ResetBindings(False)
        End If 
    
    End Sub 'button1_Click
    



    Saturday, August 22, 2015 12:56 PM
  • Below is code provided on MSDN for https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings(v=vs.110).asp.

    Not satisfactory and, unfortunately, pretty typical of MSDN.


    What are you unclear about?

    The ResetBindings method is pretty straightforward - you instruct the BindingSource to reread its .DataSource, thus updating its internal list. The boolean indicates whether or not the schema has changed (likely not).

    You might also want to look into using a BindingList(Of T), but there's nothing wrong with using as generic List(Of T) and a BindingSource.

    Explain what you're not understanding please?


    "I never failed once... It just happened to be a 2000-step process." -- Thomas Edison

    Saturday, August 22, 2015 1:04 PM
  • The ResetBindings method is pretty straightforward - you instruct the BindingSource to reread its .DataSource, thus updating its internal list.

    That's not my understanding of how it works.

    When a BindingSource is bound to an IList it does not create its own internal copy of the list.

    ResetBindings does not re-read the datasource, it simply raises the ListChanged event which tells the bound controls to update their display.  

    The ListChanged event can be raised automatically if you use a BindingList, as you have suggested, or by adding the new items to the BindingSource and not to the list.

    If you use a BindingList you don't really need the BindingSource.

    Saturday, August 22, 2015 1:54 PM

  • That's not my understanding of how it works.

    When a BindingSource is bound to an IList it does not create its own internal copy of the list.

    ResetBindings does not re-read the datasource, it simply raises the ListChanged event which tells the bound controls to update their display.  

    The ListChanged event can be raised automatically if you use a BindingList, as you have suggested, or by adding the new items to the BindingSource and not to the list.

    If you use a BindingList you don't really need the BindingSource.

    The IList interface is a collection on its own.

    "Represents a collection of objects that can be individually accessed by index."

    I don't know how else it could be done.


    "I never failed once... It just happened to be a 2000-step process." -- Thomas Edison

    Saturday, August 22, 2015 1:59 PM
  • Hello,

    Here is a working demo project and the code is shown below.

    Public Class Class1
        Public Property FirstName As String
        Public Property LastName As String
        Public Sub New()
        End Sub
    End Class
    

    ..

    Public Class Form1
        WithEvents bsData As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsData.DataSource = New List(Of Class1) From
                {
                    New Class1 With
                        {
                            .FirstName = "Karen",
                            .LastName = "Payne"
                        },
                    New Class1 With
                        {
                            .FirstName = "Anne",
                            .LastName = "Jone"
                        }
            }
    
            DataGridView1.DataSource = bsData
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CType(bsData.DataSource, List(Of Class1)).Add(
                New Class1 With
                {
                    .FirstName = "Bill",
                    .LastName = "Smith"
                }
            )
    
            bsData.ResetBindings(False)
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If bsData.Current IsNot Nothing Then
                bsData.RemoveCurrent()
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    • Proposed as answer by Frank L. Smith Saturday, August 22, 2015 2:23 PM
    • Marked as answer by trogan Saturday, August 22, 2015 10:49 PM
    Saturday, August 22, 2015 2:08 PM
  • Trogan,

    Kevin has it right, although I'd do it differently.

    I'd have the class generate a datatable (so I have more control over how the data is displayed) then I'd have the class provide the bindingsource in a function.

    If you're interested then let me know and I'll put an example together.


    "I never failed once... It just happened to be a 2000-step process." -- Thomas Edison

    Saturday, August 22, 2015 2:25 PM
  • @Torgran

    You could have simply done what is below where you set the grid.datasource = vbnull

    reset the datasource = addrs 'set it again to the list to pick up the new item added

    and Refresh()

    I had forgotten over the years, since I am a Web guy now basically, and I have not used Windows forms in several years.  You can do without all the binding stuff I saw you implementing in the form class.

    Public Class Form1

        Public addrs As List(Of DTOAddress)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            addrs = New List(Of DTOAddress)
            addrs.Add(New DTOAddress With {.Name = "Test1", .Stret = "somestreet"})
            DGV1.DataSource = addrs
        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            addrs.Add(New DTOAddress With {.Name = "Help", .Stret = "Help street"})
            DGV1.DataSource = vbNull
            DGV1.DataSource = addrs
            DGV1.Refresh()

        End Sub
    End Class

    Public Class DTOAddress

        Public Property Name As String
        Public Property Stret As String
    End Class

    Saturday, August 22, 2015 10:22 PM
  • I am not unclear about anything. I am just saying the MSDN example could be much better. I actually use MSDN a lot but am constantly frustrated by lack of/poor examples.  Thx for tip about BindingList.
    Saturday, August 22, 2015 10:49 PM
  • Thx for that very nice example.
    Saturday, August 22, 2015 10:50 PM
  • Ok, thx. I wish I didn't have to use old technology but it is a VB6 program I am converting and Windows Forms seems to be the easiest (only?).
    Saturday, August 22, 2015 10:53 PM
  • Ok, thx. I wish I didn't have to use old technology but it is a VB6 program I am converting and Windows Forms seems to be the easiest (only?).

    Sorry, can't help you with specifics in regards to VB6 to .NET as I never coded VB6 but here are some thoughts.

    If there are any third party controls in the VB6 project more likely than not you will need to first look at the standard controls available in the Framework, if something fits great, if something comes close and not sure how to proceed then ask here.

    If possible under project properties turn Option Strict On on the Compile tab, if when compiling the .NET project there are a good deal of errors then turn Option Strict Off, get things working then try turning Option Strict On and fix the errors or not but using Option Strict On is best.

    If you need to deal with data e.g. MS-Access or SQL-Server ask here which path to take rather than simply taking one because it looks easier than another.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Saturday, August 22, 2015 11:18 PM
  • I am not unclear about anything. I am just saying the MSDN example could be much better. I actually use MSDN a lot but am constantly frustrated by lack of/poor examples.  Thx for tip about BindingList.

    I use MSDN articles and serval other sources when I need to find things or examples.

    Codeproject being one.

    http://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    http://www.codeproject.com/Articles/88390/MVP-VM-Model-View-Presenter-ViewModel-with-Data-Bi

    CodeGuru being one.

    http://www.codeguru.com/

    Polymorphicprodcast being one.

    http://polymorphicpodcast.com/

    Pluralsight being one, but you have to pay.

    http://learn.pluralsight.com/learn-something-new?gclid=CNeQlJLpvccCFZKKaQodgyMCvw

    Saturday, August 22, 2015 11:23 PM
  • Just curious as to why you have marked that as an answer when functionally it is no different to the code you posted yourself earlier.
    Saturday, August 22, 2015 11:48 PM
  • Code marked as answer was very comprehensive. Thx to you all for your contributions.
    Sunday, August 23, 2015 2:32 AM
  • Actually, unless the BindingSource.AllowNew property is set to true, additions may or may not be allowed!

    This is likely a non-initialisation bug that is probably also affecting the simpler situation where no BindingSource is used (my original code). Given that it has such big consequences in terms of wasted energy, it needs to be fixed asap.

    • Edited by trogan Monday, August 24, 2015 2:30 AM
    Sunday, August 23, 2015 3:32 AM