Thursday, January 12, 2012

How to create a datatable programmatically

Hi, Today I want to discuss  about how to create a datatable programmatically.
Actually it's very easy.
We need complete next few steps:

  1. Declare DataTable
  2. Declare Columns
  3. Add columns to DataTable
  4. Fill Rows of DataTable with data if you need.
Yes, that's all.
OK, Let me show some sort of example, How I did it:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Data;

namespace HowToDataTableProgr
{
    class Program
    {
        class CompanyInfo
        {
            public CompanyInfo(string CompanyName, string CompanyAddress, double budget)
            {
                this.CompanyAddress = CompanyAddress;
                this.CompanyName = CompanyName;
                this.budget = budget;
            }
            public string CompanyName { get; set; }
            public string CompanyAddress { get; set; }

            private double budget;


            public string GetBudget()
            {
                return this.budget.ToString("C0");
            }



        }

        static void Main(string[] args)
        {

            var dataTable = new DataTable();

            var columnId = new DataColumn("Id", typeof(int));
            columnId.AutoIncrement = true;
            var columnCity = new DataColumn("City", typeof(string));
            var columnCompany = new DataColumn("compInfo", typeof(CompanyInfo));
            dataTable.Columns.AddRange(new DataColumn[] { columnId, columnCity, columnCompany });

            //add values into  rows
            dataTable.Rows.Add(null, "Washington", new CompanyInfo("Bradly co", "someStreet 12", 10000000.0));
            dataTable.Rows.Add(null, "New York", new CompanyInfo("Pooply ltd", "lane stree 45 12", 4000000.0));
            dataTable.Rows.Add(null, "Cupertino", new CompanyInfo("NewGoggle", "W Fremon AVe 2", 70000000.0));
            dataTable.Rows.Add(null, "Sidney", null);

            //Show names of Columns
            Console.WriteLine("DataTable has {0} DataColumns named:",
                dataTable.Columns.Count);
            foreach (DataColumn currentColumn in dataTable.Columns)
                Console.WriteLine("\t{0}", currentColumn.ColumnName);
            
            //show  count of rows
            Console.WriteLine("DataTable has {0} rows: ",
                dataTable.Rows.Count);
            Console.WriteLine();

            //show all data from all rows
            foreach (DataRow dataRow in dataTable.Rows)
            {
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    if (dataRow[dataColumn] is CompanyInfo)
                    {
                        var currentCompany = dataRow[dataColumn] as CompanyInfo;
                        if (currentCompany != null)
                            Console.WriteLine(string.Format("CompanyAddress:{0}, CompanyName={1}, budget={2}", currentCompany.CompanyAddress, currentCompany.CompanyName, currentCompany.GetBudget()));                        
                    }
                    else
                        Console.WriteLine(dataRow[dataColumn].ToString());
                }
                Console.WriteLine();

            }
            Console.ReadKey();
        }
    }

}

Not sure, But I think  you have a few questions regarding source code.
Why so complex? you can ask me.
You see, right, If you need create  DataTable  and that's all, then it should looks like:

1
2
3
4
5
6
7
8
var dataTable = new DataTable();//declare DataTable

            var columnId = new DataColumn("Id", typeof(int));//declare column
            columnId.AutoIncrement = true;//set as columnId.AutoIncrement
            var columnCity = new DataColumn("City", typeof(string));//declare column
            var columnCompany = new DataColumn("compInfo", typeof(CompanyInfo));//declare column

            dataTable.Columns.AddRange(new DataColumn[] { columnId, columnCity, columnCompany });//add Range of columns to existed DataTable



No comments:

Post a Comment