Subscribe by Email

Your email:

Posts by Month

Follow Me

Current Articles | RSS Feed RSS Feed

Working with a DataTable in C# for beginners

 

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.

C

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:

C

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.

C# Get Data

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.

C# Dat Grid View

And that’s all there is to using a DataTable in C#.

Have you worked with DataTables? Submit your experiences and comments below!

All Posts