Posted by
Josh Hrach on Tue, Feb 07, 2012 @ 01:25 PM
One programming language that I have been focusing on lately is C#. If it's not your native programming language, it can help you become more versatile when it comes to software development.
Working with C#, you may have the need to take data and enter it into a data grid. If still learning the art of the language, I figured this was something you may want to try to perfect.
Creating a data table in C# isn’t that difficult. In this example, I took data from a Microsoft Access database and entered it into a DataTable object.
First, I defined the DataTable and its columns.

While it isn’t necessary, I like having temporary variables to hold data from a database before entering it into my table. For what we have, that would be:

All that is left to get is the data itself. Assuming we’re getting it from some kind of database, we can get the information in a while loop and add all of the data into the table.

Now we have our data in the DataTable. All that is left is to output it. For this, I have a DataGridView object in my form called myDataGrid.

And that’s all there is to using a DataTable in C#.
Have you worked with DataTables? Submit your experiences and comments below!