how to add row to datagrid and datasource....

After populating my datagrid from a list(of myclass), I want the user to be able to add additional rows by clicking my Add button.   I tried using the datagrids addrow but I got an error that I can't add rows to a databound datagrid.  Instead, I need to add them to the datasource and then updated the grid, but the attached code does not work.   I start with 153 records for the chk value, and I end with 153 records for the chk value.   What am I overlooking?
Dim fc As New HPFFAFormClass
        Dim chk As Integer = 0


        chk = DataGridViewForms.RowCount

        fc.HPFNO = 1000
        fc.FormName = "New Form"
        fc.HPFDocType = ""
        DataGridViewForms.DataSource.add(fc)
        DataGridViewForms.Refresh()

        chk = DataGridViewForms.RowCount

Open in new window

codefingerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Try this way:
        Dim dv As DataView = DirectCast(Me.DataGridViewForms.DataSource, DataView)
        Dim row As DataRow = dv.Table.NewRow
        row.Item("name of the field") = "some value
        ' ...
        dv.Table.Rows.Add(row)

Open in new window

0
CodeCruiserCommented:
>After populating my datagrid from a list(of myclass)

Then add new item to the list

Dim obj As New myclass
obj.propertyname=value
...
Mylist.Add(obj)
0
codefingerAuthor Commented:
No suggestion I have tried works for me....honestly, I did not expect such a simple standard thing to be so freaking difficult!

For CodeCruisers suggestion, nothing happens visually to the datagrid -- it does not get a new row on which the user can type values.

For jpaulino's suggestion, I get an error:
Unable to cast object of type 'System.Collections.Generic.List`1[HPF_Forms_Assistant.HPFFAFormClass]' to type 'System.Data.DataView'.

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

CodeCruiserCommented:
Try reassigning the datasource after adding the row to the list. So

Dim obj As New myclass
obj.propertyname=value
...
Mylist.Add(obj)

DataGridView1.DataSource = Mylist
0
codefingerAuthor Commented:
No luck.  See attached.   There is still no visual indication on the datagrid that a row has been added, and no place for the user to type or override values on the bound object.  I have tried it with and without Refresh, same result.


Dim fc As HPFFAFormClass = Nothing
        Try
            fc = New HPFFAFormClass
            fc.HPFNO = 1000
            fc.FormName = "New Form"
            fc.HPFDocType = "Unknown"
            fc.Duplex = "N"
            fc.Saved = False
            ctrl.frmlist.Add(fc)
            Me.DataGridViewForms.DataSource = ctrl.frmlist
            Me.DataGridViewForms.Refresh()

        Catch ex As Exception

            MsgBox(ex.Message)


        End Try

Open in new window

0
CodeCruiserCommented:
Strange. Another attempt

From
Me.DataGridViewForms.DataSource = ctrl.frmlist

to

Me.DataGridViewForms.DataSource = Nothing
Me.DataGridViewForms.DataSource = ctrl.frmlist
0
codefingerAuthor Commented:
That works, but it means I have to completely redraw the grid  --- hide the columns I don't want the user to see, repopulate an unbound drop down combo box, repopulate it and set its values according to the user's earlier choices....each time the user clicks the Add button.....kludgy, but it works.

Its hard to believe there is not a better solution.






0
CodeCruiserCommented:
The trouble is that there is no DataBind() method in winforms grid as there is one in ASP.NET.
0
codefingerAuthor Commented:
.I knew there had to be a better way.. It took me more hours of Googling to track this down, but it WORKS, and does NOT require me to redraw the datagrid....

Populate the grid this way:
------------------------------------------
dim bs as new BindingSource
bs.DataSource = list(of myclass)
mydatagrid.datasource = bs

Add a new object like this:
-----------------------------------
bs.Add(myclass)

So what do I get for outsmarting the "experts"?  
 (Sorry, could not resist gloating a little.)







Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.