Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add new row to bound datagridview

Status
Not open for further replies.

johnc83

Technical User
Jan 29, 2008
154
GB
Hi all,

got a problem and I'm starting to think what I am trying to achieve may not even be possible..

I want to programatically add a new row to my bound datagrid view. I've read a lot of times that I need to add the new row to my binding source as opposed to my datagridview.

Will this mean that the row is added to the database at that point because ideally I would prefer to add many rows then commit them to the database.

Would someone mind showing me how to do that (if its even possible). My current code is below but because I don't fully understand how it works, it's throwing up errors.

Code:
  Dim dt = TblJobsBindingSource
        Dim dr As DataRow = dt.NewRow
        dr("JobNumber") = "12345"
        dr("Customer") = "Dave"
        dt.Rows.Add(dr)

thanks in advance for any replies.

John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 


Code:
Dim dt [b]As DataTable[/b] = TblJobsBindingSource
Dim dr As DataRow = dt.NewRow
dr("JobNumber") = "12345"
dr("Customer") = "Dave"
dt.Rows.Add(dr)
 
Hi RiverGuy, thanks for the reply

unfortunately, this brings the following error:

type 'System.Windows.Forms.BindingSource' cannot be converted to 'System.Data.DataTable'.

Any ideas what the workaround could be?

Thanks

John


.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
I am assuming you are using a DataTable as the DataSource for your grid since you are using the "dt" acronym and the .NewRow method. You need to add the row to the underlying DataTable.
 
Hi,

sorry for confusion (I copied that code from some other part of my app). I am using a bindingsource as the datasource of my datagridview.

Is it possible to add the row and then commit to the DB when I come to save the full job?

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
Hi again, think im getting closer to it but need help on one last thing..

Code:
 MyDataGridView.DataSource.AddNew()
        MyDataGridView.Rows(rowcount - 1).Cells(0).Value = "12345"

This adds a new row and inserts the job number into the correct column. Only problem is that when I save the job it loses that job number. However, if I was to simply click the mouse on the row I just added and then save, it retains the data so basically I am looking for someone thing like..

MyDataGridView.CurrentRow.Validate

but there doesn't seem to be anything like this..

hope you can help (so close :))

Thanks

John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
Well, it still leads me to believe that the .DataSource for the BindingSource is a DataTable. Is that correct? You can still access and add a new row to the .DataSource of the BindingSource. Something like:

Dim dr As DataRow = CType(YourBindingSource.DataSource, DataTable).NewRow

dr.Item(0) = SomeValue
dr.Item(1) = SomeOtherValue

CType(YourBindingSource.DataSource, DataTable).Rows.Add(dr)
 
Hi, thanks for your time on this - really appreciated.

I tried your way and it comes up with the error

Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.

This is how my setup works

TblJobsDataGridView datasource is RETblJobsBindingSource

RETblJobsBindingSource datasource is REParentTableBindingSource

so seems no datatable in site? unless Im not understanding something which seems very likely!!

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
Got it sorted!! Here is the code for anyone interested which lets me programatically add a new row (along with values) to my bound datagridview..

Code:
'Count how many rows there are - default is 1 because that is the new row which a user could manually type in
Dim rowcount As Integer = TblJobsDataGridView.Rows.Count
'Add new row to the datagridview's datasource
TblJobDataGridView.DataSource.AddNew()
'Set the value of column 1 on the new row
TblJobDataGridView.Rows(rowcount - 1).Cells(0).Value = "1"
'Set the value of column 2 on the new row
TblJobDataGridView.Rows(rowcount - 1).Cells(1).Value = "2"
'Commit the line
TblJobDataGridView.DataSource.endedit()

thanks for your help

John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top