Solved

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

Posted on 2016-10-03
8
40 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 40
Creating a route in asp.net webforms 2 26
write xml in vb.net 2 24
vb.net class 3 14
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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