Labels

slider

Recent

Navigation

DataTable in C#

DataTable, Dataset, DataRow, DataColumn, Transponse Datatable, DataTable in C#

Introduction

DataTable is formed with rows and columns, it stored the data in the same way as stored others objects in C#.

Namespace

If we are dealing with DataTable in C# then we require to define following Namespace in our C# program.

using System;

using System.Data;

Here demonstrating to get columns name from a DataTable.

private void Get_Column_name()
{
            // Declare data table
            DataTable _ dtTC = new DataTable();
             
          // get column name using with foreach loop
           foreach(DataColumn _Cols in _dtTC.Columns)
            {
                string _str_Col_name = _Cols.ColumnName;
            }

}

DataTable In C#

Another method to select data from a specific DataTable in C#.

        private void Select_Data_From_Datatable_In_CSharp()
        {
            // Declare data table
            DataTable _dtSelectTC = new DataTable();

            // add column to data table
            _dtSelectTC.Columns.Add("ID", typeof(Int32));

            // adding rows to data table
            _dtSelectTC.Rows.Add(10);
            _dtSelectTC.Rows.Add(20);
            _dtSelectTC.Rows.Add(30);
            _dtSelectTC.Rows.Add(40);
            _dtSelectTC.Rows.Add(50);

            // filtering data on demand
            DataRow[] _resultTc = _dtSelectTC.Select("ID > 30");

            // Display results
            foreach (DataRow _rowTc in _resultTc)
            {
                Console.Write(_rowTc[0] + "\n");
            }

            // Output
            // 40
            // 50
        }

Conclusion

I have explained in detail about DataTable in C#, it uses system.Data to define DataTable and further uses of its properties, constraints etc. We can use “Clear “ method to clear data from DataTable, “Clone” helps us to cloning of a DataTable with structure and data, “Copy” copies its data with schema.

Suggested Reading:



Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

1 comments: