DataSet And DataTable

DataSet And DataTable

You can say that Dataset and data table Are DataContainers.Data table generally represents the data inside of a grid format. They can be used to show images, text, numerals and even hyperlink. We can create data table column object by using add method.

Using system. Data is the namespace used for the Data table.

Creating an object for the data table and creating columns for it.
There may be times when we need to build a Data Table using code instead of retrieving the data from a database and filling the data table.

Here is the Code for creating an data table

       DataTable dtDatatable = new DataTable();
       dtDatatable.Columns.Add("Column1", typeof(int));
       dtDatatable.Columns.Add("Column2", typeof(string));

To create a new Row use newrow method

        DataRow Row = dtDatatable.NewRow();
        Row["Column1"] = 1;
        Row["Column2"] = "BillGates";

It is Easy to sort.

An example which shows how to sort using a data table

        DataTable dt = dtDatatable.Clone();
        DataView dataview = dtDatatable.DefaultView;
        dataview.Sort = "Column1 DESC";
        dt = dataview.ToTable();

      DataTable.copy and clone

Both the Copy and the Clone methods create a new Data Table with the same structure as the original Data Table. The new Data Table created by the Copy method has the same set of Data Rows as the original table, but the new Data Table created by the Clone method does not contain any Data Rows.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: