.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Converting array to data table

Posted By:nishithraj       Posted Date: November 30, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Converting array to data table.

We can add an array to the data table directly if we require as shown in the example. Here we are converting each array value to  data table value.
 


if (!Page.IsPostBack)
        {
            dt= new DataTable();
            DataRow dr;
            dt.Columns.Add(new DataColumn("Employee name", typeof(string)));
            dt.Columns.Add(new DataColumn("Age", typeof(string)));
            string[,] employees= new string[5, 2] { { "John", "28" }, { "Jose", "32" }, { "Suresh", "34" }, { "Nathan", "37" }, { "Arvind", "39" } };
            for (int i = 0; i < 5; i++)
            {
                dr = dt.NewRow();
                dr[0] = employees[i, 0];
                dr[1] = employees[i, 1];
                dt.Rows.Add(dr);
            }
            gvwProducts.DataSource=dt;
            gvwProducts.DataBind();
        }

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend