i want to convert datatable to list in c# please help me.

1 Answers

  1. Aditya Kumar Patel
    convert datatable to list in c# Asp.Net
    8/2/2018 10:24:14 AM

    Try to follow these step to convert Datatable to List in Asp.Net c#

    Creating a simple Student class like:

    public class Student
        {
            public int StudentId { get; set; }
            public string StudentName { get; set; }
            public string Address { get; set; }
            public string MobileNo { get; set; }
        } 

    And a DataTable with some data like:   

                DataTable dt = new DataTable("Student");
                dt.Columns.Add("StudentId", typeof(Int32));
                dt.Columns.Add("StudentName", typeof(string));
                dt.Columns.Add("Address", typeof(string));
                dt.Columns.Add("MobileNo", typeof(string));
                //Data              dt.Rows.Add(1, "Abishek", "Hyderabad", "0000000000");
                dt.Rows.Add(2, "Pawan", "Hyderabad", "111111111");
                dt.Rows.Add(3, "Namit", "Pune", "1222222222");
                dt.Rows.Add(4, "Neha", "Bhagalpur", "3333333333");

    Create a generic method that will convert any type of DataTable to a List.

    Note: DataTable structure and List class structure should be the same.

    private static List<T>ConvertDataTable<T>(DataTable dt)
            {
                List<T> data = new List<T>();
                foreach (DataRow row in dt.Rows)
                {
                    T item = GetItem<T>(row);
                    data.Add(item);
                }
                return data;
            }
            private static T GetItem<T>(DataRow dr)
            {
                Type temp = typeof(T);
                T obj = Activator.CreateInstance<T>();
     
                foreach (DataColumn column in dr.Table.Columns)
                {
                    foreach (PropertyInfo pro in temp.GetProperties())
                    {
                        if (pro.Name ==column.ColumnName)
                            pro.SetValue(obj, dr[column.ColumnName], null);
                        else
                            continue;
                    }
                }
                return obj;
            } 

    To call the ConvertDataTable method on button Click, use the following Code:

     

      List<Student> StudentList = new List<Student>();
      StudentList = ConvertDataTable<Student>(dt);

     

    For Example:

    protected void btnConvertDatatableToList_Click(object sender, EventArgs e)
            {            
                DataTable dt = new DataTable("Student");
                dt.Columns.Add("StudentId", typeof(Int32));
                dt.Columns.Add("StudentName", typeof(string));
                dt.Columns.Add("Address", typeof(string));
                dt.Columns.Add("MobileNo", typeof(string));
                //Data 
                dt.Rows.Add(1, "Abishek", "Hyderabad", "0000000000");
                dt.Rows.Add(2, "Pawan", "Hyderabad", "111111111");
                dt.Rows.Add(3, "Namit", "Pune", "1222222222");
                dt.Rows.Add(4, "Neha", "Bhagalpur", "3333333333");
     
                List<Student> StudentList = new List<Student>();
                StudentList = ConvertDataTable<Student>(dt);
            }

     

    I hope it will help to you.


Leave a answer

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like