Question adding item to already created dataset and datatable

jithin

New member
Joined
Dec 1, 2012
Messages
1
Programming Experience
Beginner
I created a dataset in visual studio from Project->add new item also added a datatable in that dataset. I want to add items to that datatable from a text field with a button click and display it on a datagridview. i need the c# code to do this.

thanks.....Sorry for my english
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
Just like adding a form or whatever else to your project, all you've done is create a class. If you want to use that class at run time then you still have to create an instance. Once you create an instance of your DataSet class, it will have a property for each DataTable, with. Each DataTable will have a method to create a new DataRow. That row will then have a property for each column. The table will then have a method to add that new row. I'm not going to provide the code because all the class and method names will be specific to your application, which is the whole point of a typed DataSet. Here's the list of steps though, which are all very simple:

1. Create a new instance of your DataSet.
2. Get the desired DataTable from the appropriate property of the DataSet.
3. Call the appropriately-named method of the DataTable to create a new DataRow.
4. Set all the properties of the row that correspond to the columns of the table.
5. Call the appropriately-named method of the DataTable to add the new DataRow.

To display the contents of the DataTable in a DataGridView, assign the table to the DataSource property of the grid.

You can do all this in code if you want but you can also do some of the work in the designer. If you look at the top of the Toolbox you should see that your DataSet appears there. You can add an instance to your form there instead of creating it in code. That means that you can also bind it to the DataGridView in the designer, assigning the DataSet to the grid's DataSource and then selecting the appropriate DataTable as the DataMember.
 
Top Bottom