Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Accessing Data
 How to: Add Rows to a DataTable

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Data Access in Client and Middle-Tier Programming 
How to: Add Rows to a DataTable 

To add new records into a dataset, a new data row must be created and added to the DataRow collection (Rows) of a DataTable in the dataset. The following procedures show how to create a new row and insert it into a DataTable. Examples are provided for both typed and untyped datasets.

NoteNote

Applications that use data-bound controls typically get the ability to add new records through the "add new" button on a BindingNavigator Control.

Inserting a New Record into a Typed Dataset

For this example, it is assumed that a dataset has a Customers DataTable and has two columns named CustomerID and CompanyName. Typed datasets expose the column names as properties of the typed DataRow object; in this case the CustomersRow.

To add a new record to a typed dataset

  • Declare a new instance of the typed dataset. In the following example, you declare a new instance of the CustomersRow class, assign it a new row, populate the columns with data, and add the new row to the Customers table's Rows collection:

    Visual Basic
    Dim newCustomersRow As NorthwindDataSet.CustomersRow
    newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
    
    newCustomersRow.CustomerID = "ALFKI"
    newCustomersRow.CompanyName = "Alfreds Futterkiste"
    
    NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)
    
    C#
    NorthwindDataSet.CustomersRow newCustomersRow = 
        northwindDataSet1.Customers.NewCustomersRow();
    
    newCustomersRow.CustomerID = "ALFKI";
    newCustomersRow.CompanyName = "Alfreds Futterkiste";
    
    northwindDataSet1.Customers.Rows.Add(newCustomersRow);
    
    J#
    NorthwindDataSet.CustomersRow newCustomersRow;
    
    newCustomersRow = northwindDataSet1.get_Customers().NewCustomersRow();
    newCustomersRow.set_CustomerID("ALFKI");
    newCustomersRow.set_CompanyName("Alfreds Futterkiste");
    
    northwindDataSet1.get_Customers().get_Rows().Add(newCustomersRow);
    

Inserting a New Record into an Untyped Dataset

For this example, it is assumed that the untyped dataset has a Customers DataTable that has two columns named CustomerID and CompanyName. Untyped datasets require knowledge of column names or indices when coding. This example uses column names.

To add a record to an untyped dataset

  • Call the NewRow method of a DataTable to create a new, empty row. This new row inherits its column structure from the data table's DataColumnCollection. The following code creates a new row, populates it with data, and adds it to the table's Rows collection.

    Visual Basic
    Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow()
    
    newCustomersRow("CustomerID") = "ALFKI"
    newCustomersRow("CompanyName") = "Alfreds Futterkiste"
    
    DataSet1.Tables("Customers").Rows.Add(newCustomersRow)
    
    C#
    DataRow newCustomersRow = dataSet1.Tables["Customers"].NewRow();
    
    newCustomersRow["CustomerID"] = "ALFKI";
    newCustomersRow["CompanyName"] = "Alfreds Futterkiste";
    
    dataSet1.Tables["Customers"].Rows.Add(newCustomersRow);
    
    J#
    DataRow newCustomersRow = 
        dataSet1.get_Tables().get_Item("Customers").NewRow();
    
    newCustomersRow.set_Item("CustomerID", "ALFKI");
    newCustomersRow.set_Item("CompanyName", "Alfreds Futterkiste");
    
    dataSet1.get_Tables().get_Item("Customers").get_Rows().Add(newCustomersRow);
    

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
is this a mistake ?      Nicolas Mach ... Joel Mussman   |   Edit   |   Show History
Sorry, i'm re-learning VB.

But, is this a mistake ?

Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
Why is NortwindDataSet and NorthwindDataSet1 in the sample ??

[tfl - 13 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content.
For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&


Interesting that this response came months later, but just a day before I read it. I agree with tfl that the question should be asked in a forum, but why make future readers dig when they read these comments?

It is not a mistake . The first line is setting the data type of the variable to a particular class. The second line is creating a new instance of a row using a method of the class through a specific instance (NorthwindDataSet1). This should answer the next comment below as well.

need more explanation      Saim Pasha ... Thomas Lee   |   Edit   |   Show History
NorthwindDataSet.CustomersRow newCustomersRow = 
    northwindDataSet1.Customers.NewCustomersRow()

please explain the second line of code

[tfl - 13 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content.
For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
C++      rdb7051 ... Thomas Lee   |   Edit   |   Show History

I'm a newbie and am trying to reproduce the code in Forms C++. Haven't been able to get it right. Can anyone show the code samples as they would appear in C++?

[tfl - 13 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content.
For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker