Exporting data from a datagrid column to a string

I have three columns in my datagrid, one for usernames, another for passwords, and one last for e-mail addresses.

I'd like to save the usernames column to a text file, so how do I grab the data from that column and slap it into a string?

I don't want to declare the dataset as a global, although I suppose I could and that would halfway work, but anything typed into the datagrid in that case would be ignored.

In short: I want to loop through the datagrid's username column and populate a string with it's contents. Is there a way to treat the dg as an array?



LVL 2
SootahAsked:
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.

Bob LearnedCommented:
ASP.NET or WinForms?  .NET version?  2002, 2003 or 2005?

Bob
SootahAuthor Commented:
C# .NET 2005
SootahAuthor Commented:
Oh, it's a Windows application.
YurichCommented:
// if you have your DataGrid dataGrid1 (and if it's actually WIndows application)
string userName = dataGrid1[ 1, 2 ].ToString();

that will give you field localted in the row 2 (1), column 3 (2), you can set it to proper values.

knowing that, iterating through the datagrid doesn't seem to scary, BUT you need to know the total number of rows and for datagrid it's not trivial - there's a way to do it through binding context, but I don't remember it and there're quite of few casting from type to type. I normally use a DataTable or DataSet for my datagrid, knowing that, you can cast DataSource back to its orginal type and get the number of rows:

int count = (( DataTable )dataGrid1.DataSource ).Rows.Count;

having this in your disposal, you can iterate throu your datagrid

ArrayList usernames = new ArrayList();
for( int i = 0; i < count; i ++ )
{
  string userName = dataGrid1[ i, 2 ].ToString();
  usernames.Add( userName );
}

then you can save the contents of your ArrayList into the file, alternatively, you can do it right in the for loop:
// presuming that you have declared your filestream and streamwriter before:
...
{
  // sr - is a stream writer
  sw.WriteLine( dataGrid1[ i, 2 ].ToString());
}

good luck,
yurich
 

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