0

I have 60 columns and unknown rows. I'm getting the datas continuously such as Current.Cursor.Position.X and Current.Cursor.Position.Y and many of them. So there is no unknown numbers of rows. I want to save these datas nicely. I dont want to be busy with db. I'm new on this topic. I tried to save them in a text file. I was successfull, but that wasn't in a order. I want to make it in order. What could be the perfect solution for this? If you can provide example codes, It will be perfect for me to understand better.

        System.Data.DataTable DT = new System.Data.DataTable();

        DT.Columns.Add( " 1 ");
        DT.Columns.Add("Column2");
        DT.Columns.Add("Column3");
        DT.Columns.Add("Column60");


           DT.Rows.Add(skeleton.Joints[JointID.Head].Position.X,skeleton.Joints[JointID.Head].Position.Y, skeleton.Joints[JointID.Head].Position.Z);


        foreach (DataRow row in DT.Rows)
        {

            StreamWriter fileWriter = new StreamWriter("table.csv",true);
            fileWriter.WriteLine(row[0].ToString() + row[1].ToString() + row[2].ToString());
            fileWriter.Close();

            }
3
  • There's only one perfect solution: a DBMS. Commented Feb 14, 2012 at 16:48
  • You want to save it nicely but what then? How do plan to use the data? Commented Feb 14, 2012 at 16:51
  • I'm not gonna use it. Just to show it. I will not retrieve it or I will not send it. I will just store them to be shown later. Commented Feb 14, 2012 at 17:09

1 Answer 1

1

This code should get you starting

//create an instance of a table
System.Data.DataTable DT = new System.Data.DataTable();
//dynamically add columns
DT.Columns.Add("Column1");
DT.Columns.Add("Column2");
DT.Columns.Add("Column3");
.
.
.
DT.Columns.Add("Column60");

//this is how you add rows to it
DT.Rows.Add("val1", "val2", "val3",...."val60");

//this is how you retrieve it
foreach (DataRow row in DT.Rows)
{
   Console.Writeline(row[0].toString()); //returns the first column for each iteration
}

Hope this was helpful to you.. dont forget to vote up

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this solution, but I have to get it as an output file. Not on console. Also, in this code, row numbers are certain. But in my project, row numbers are not certain. Any idea?
So write it to a file. What was supplied a sample application. Writting to a file is trivial. Actually in this example row numbers are NOT certain, take a look again, if you still don't understand open a book on .NET
Ramhound was correct.. what i did was created a column for example up to 60 columns... and the rows you can add as many as you need... then you can change the console.writeline to your code like writing to a file...
@Ramhound: Thanks for your advice, but you shouldnt consider me as a professional C# developer. If I can understand everything at one glance, I dont ask here. I ask here to get ideas and in the sense of learning. For Philip: I meant this by saying "not certain": what If I have 500 rows, then I need to write 500 vals there. My rows are changing each time application starts.I was trying to get the answer for this. Thank you also for your advice, I will try these.
@Samet you can do this as many times as you want... DT.Rows.Add("val1", "val2", "val3",...."val60"); it ads 1 ROW with 60 columns... so you just need to loop to how many times you want
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.