Solved

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

Posted on 2016-10-03
8
45 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
make control visible based off other control value 7 45
CSS question 16 63
SSIS Column mapping 5 42
VB Catch Statement in Event Viewer. 2 20
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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