How to add this in to data grid view ?

i am using .net frame work 4.5 with c# (windows application).

I have the following string.

String mystring="1.London|2.New York|3.Hong Kong";

I need to bind the above values in the data grid view in the  following row format. How to achieve this ?

2.New York
3.Hong Kong
Varshini SAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Did you mean something like:
using System;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q28647698
	public partial class Form1 : Form
		private const string data = "1.London|2.New York|3.Hong Kong";

		public Form1()

		private void OnLoad(object sender, EventArgs e)
			var locations = data.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries).Select(pair => new { ID = pair.Split('.')[0], Location = pair.Split('.')[1]}).ToList();
			dgvData.DataSource = locations;

Open in new window

Which produces the following output -Capture.JPG-saige-
Fernando SotoRetiredCommented:
Hi Varshini S;

The following code snippet will do what you want.

// Test Data
String mystring = "1.London|2.New York|3.Hong Kong";
// A Linq query to iterate through the individual string and wrap the each string into an Anonymous object
// type so that the single string in each object gets displayed correctly as a string and not the length of each individual string.
var results = mystring.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Select(s => new { s }).ToList();
// Now bind it to the DataGridVire Object.
dataGridView1.DataSource = results;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi Varshini S;

If you were to bind an array of string's directly to the DataSource of the DataGridView it will use the first public property of the String class, which is the Length property as the value to be displayed. To get around this I wrapped each string around an Anonymous type so that it displays the string and not the length.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Fernando SotoRetiredCommented:
By the way if you want the DataGridView header to be Location then change this part of the code,
Select(s => new { s })

To this

Select(Location => new { Location })

Open in new window

Or change Location to whatever you want the header to be.
Isn't that the same thing I did Fernando?  ;)

Fernando SotoRetiredCommented:
Also in my first post with the solution I had refactored the code but somehow cutting and pasting I placed the wrong line. Please replace this line in my original post
var results = mystring.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries).Select(Location => new { Location }).ToList();

Open in new window

Fernando SotoRetiredCommented:

Not exactly, the Anonymous type you created has two objects in it. From what I understood it was to be one string like, 1.London, having a digit a dot and the location, but I may be wrong.
You are correct sir.  My mistake... ;)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.