Showing posts with label Datatable. Show all posts
Showing posts with label Datatable. Show all posts


DataTable in C#


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


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

            // 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


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:


Transpose Datatable C#


Dataset and datatable are the lifelines of (c#). Without data table & dataset we are short, inflexible, expensive, unmanageable and less availability. Recently I dealt greatly with transposed datatable in C#. Data was throwing me in vertical and I need to manipulate data back into horizontally. This is very time consuming to manage the data properly according to our situation, so it needs me to manage transpose datatable in c# according to situation; moreover I motivated to share this one in detail. I have provided steps how can we generate transpose datatable in c# as below:
using System.Data;
Step 1: In first step, I have added some required .net controls to view transpose data table in Gridview.

Transponsed datatable Html View


How to pick distinct rows from datatable in


How to pick distinct rows from Datatable in, below is written code snippet to go through this one.
Firstly declare default view and dv.ToTable(True,Param1,Param2,Param3), and we can pass the parameter to unique column.

Vb.Net code snippet

  Dim dv As New DataView
  dv = dt.DefaultView
  Dim uniquetabel As DataTable = dv.ToTable(True, "Column1", "Column2")