Solved

Help with assigning value of previous row to new row added in DataGridView

Posted on 2016-10-03
8
43 Views
Last Modified: 2016-10-04
Hi,

When adding data in a new row in my DataGridView, I need to automatically assign the COUNTRY column, country value from the previous row or a value from a textbox. How do I achieve this using VB.NET?  Also , How save the record only if more than one column is populated in new row added?

Thanks,

Victor
0
Comment
Question by:vcharles
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41827399
Sample example  !! Enjoy !!

Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow()

newCustomersRow("CustomerID") = "Pawan1312"
newCustomersRow("COUNTRY") = "United States"

DataSet1.Tables("Customers").Rows.Add(newCustomersRow)

DataSet1.Tables(0)(ds.Tables(0).Rows.Count - 1)("COUNTRY") = "United States"
0
 

Author Comment

by:vcharles
ID: 41827400
Hi,

Which method of thr Grid do I include the code?

Thanks,

Victor
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41827403
You can internally use datatable.

You must be using the datatable as a source to a GridView.  You have to use button click event

When you add a new row in GridView , add a row in the table just as mentioned above. After that change the previous row's country

after that again assign that datatable to the GridView
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41827407
May be something like below

Create a new button, on click on that event call below-

Protected Sub AddNewRecord(ByVal sender As Object, ByVal e As EventArgs )
     
	--Get datatable from session
	
	Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow()

	newCustomersRow("CustomerID") = "Pawan1312"
	newCustomersRow("COUNTRY") = "United States"

	DataSet1.Tables("Customers").Rows.Add(newCustomersRow)

	DataSet1.Tables(0)(ds.Tables(0).Rows.Count - 1)("COUNTRY") = "United States"
	 
    gridView.datasource = DataSet1.Tables(0);
	
	BindData();            

End Sub

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41827491
Hi, Do you need more info on this?
0
 

Author Comment

by:vcharles
ID: 41827750
Hi

Will test code this morning and get back to you..

Thanks,

Victor
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41827771
Here is a sample VB.Net project for you!



Enjoy !
Demo.zip
0
 

Author Closing Comment

by:vcharles
ID: 41827786
Thank you.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question