Tuesday, 24 May 2016

Transfer List Collection Values To DataTable In C#.Net

5/24/2016 - By Pranav Singh 0

This article will show you how you can transfer datatable row values to class list collection using C#.net and VB.Net.


First prepare a class file and add the below code into the class.

  public class CountryDetail
    {
        public int Id { get; set; }
        public string CountryName { get; set; }
        public string Population { get; set; }
        public string Code { get; set; }
    }

So here is the code for the list collection. You can fill your datatable with your sql database table value also.

  public List<CountryDetail> GetCountryData()
        {
            List<CountryDetail> countryDetail = new List<CountryDetail>();
            countryDetail.Add(new CountryDetail { Id = 1, CountryName = "India", Code = "IN", Population = "125Cr" });
            countryDetail.Add(new CountryDetail { Id = 2, CountryName = "Pakistan", Code = "PK", Population = "50Cr" });
            countryDetail.Add(new CountryDetail { Id = 3, CountryName = "America", Code = "US", Population = "24Cr" });
            return countryDetail;
        }

After this I will show you the way to transfer the list collection value to datatable. Now check the below code.

  public DataTable AssignListValueToDataTable(List<CountryDetail> countryDetail)
        {
            DataTable dt = new DataTable();
            dt.Clear();
            dt.Columns.Add("CountryName");
            dt.Columns.Add("Population");
            dt.Columns.Add("Code");
            foreach (CountryDetail item in countryDetail)
            {
                DataRow dataRow = dt.NewRow();
                dataRow["CountryName"] = item.CountryName;
                dataRow["Population"] = item.Population;
                dataRow["Code"] = item.Code;
                dt.Rows.Add(dataRow);
            }
            return dt;
        }

In above code I have shown you to first read the list collection and after that add the value into the datatable

Now check the page load code to activate the transfer code.

protected void Page_Load(object sender, EventArgs e)
        {
            List<CountryDetail> countryDetail = new List<CountryDetail>();
            countryDetail = GetCountryData();
            //Now prepare instance oif datattable
            DataTable dt = new DataTable();
            dt = AssignListValueToDataTable(countryDetail);
        }

Check the final output in debug mode. Have a look the below output. 


Note: Please download the demo to view the combined code to this article

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top