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 ?

1.London
2.New York
3.Hong Kong
Varshini SAsked:
Who is Participating?
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.

it_saigeDeveloperCommented:
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()
		{
			InitializeComponent();
		}

		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-
0
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

0

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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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.
0
it_saigeDeveloperCommented:
Isn't that the same thing I did Fernando?  ;)

-saige-
0
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

0
Fernando SotoRetiredCommented:
@-saige-

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.
0
it_saigeDeveloperCommented:
You are correct sir.  My mistake... ;)

-saige-
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.