Uncategorized

Create Dynamic DataTable in C#

Many a time , we need to create DataTable dynamically inside our C# Code . So this is the easiest way how we can create DataTable dynamically and add data to it .

DataTable dtDatabaseInfo = new DataTable("tblData");
            dtDatabaseInfo.Columns.Add("ID", typeof(string));
            dtDatabaseInfo.Columns.Add("Destination", typeof(string));
            dtDatabaseInfo.Columns.Add("ServerName", typeof(string));
            dtDatabaseInfo.Columns.Add("sDB_Name", typeof(string));
            dtDatabaseInfo.Columns.Add("DbUserName", typeof(string));
            dtDatabaseInfo.Columns.Add("DbPassword", typeof(string));

            DataRow row = dtDatabaseInfo.NewRow();

            row["ID"] = "M001";
            row["Destination"] = "MyDestination";
            row["ServerName"] = "MyLocalServer";
            row["sDB_Name"] = "MyDatabase";
            row["DbUserName"] = "MyUser";
            row["DbPassword"] = "MyPassword";
            dtDatabaseInfo.Rows.Add(row);

Standard