Solved

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

Posted on 2016-10-03
8
35 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 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Expert Comment

by:Pawan Kumar Khowal
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
 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Expert Comment

by:Pawan Kumar Khowal
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now