Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-03
8
Medium Priority
?
47 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 30

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 30

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Accepted Solution

by:
Pawan Kumar earned 2000 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 30

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 30

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

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.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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