Tuesday, May 27, 2014

How to add columns and row in datatable.

A simple way to adding columns and rows in Datatable
// Create a new Datatable
            DataTable dtEmployee = new DataTable("Employee");
           
            // Define Column
            DataColumn DColEmpID = new DataColumn("Emp_ID", System.Type.GetType("System.Int32"));
            DColEmpID.AllowDBNull = false;
            DColEmpID.Caption = "ID";
            DColEmpID.AllowDBNull = false;

            //Adding Columns
            dtEmployee.Columns.Add(DColEmpID);


            DataColumn DColName = new DataColumn("Emp_Name", System.Type.GetType("System.String"));
            DColName.AllowDBNull = false;
            DColName.Caption = "Name";
            dtEmployee.Columns.Add(DColName);

          
            DataColumn DColDept = new DataColumn("Department_ID", System.Type.GetType("System.Int32"));
            DColDept.AllowDBNull = false;
            DColDept.Caption = "Department_ID";
            DColDept.DefaultValue = "1";
            dtEmployee.Columns.Add(DColDept);

           
            DataColumn DColSalary = new DataColumn("Salary", System.Type.GetType("System.Decimal"));
            DColSalary.Caption = "Salary";
            DColSalary.DefaultValue = "0.0";
            dtEmployee.Columns.Add(DColSalary);

            //Add data into columns
            dtEmployee.Rows.Add("1", "Jacky", "21", "5000");

            dtEmployee.Rows.Add("2", "Franky", "21", "7000");

No comments:

Post a Comment

Search This Blog