[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1071
  • Last Modified:

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

0
codefinger
Asked:
codefinger
  • 4
  • 4
1 Solution
 
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now