Answers for "c# datatable"

4

C# create DataTable

DataTable dt = new DataTable(); 
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");

DataRow _ravi = dt.NewRow();
_ravi["Name"] = "ravi";
_ravi["Marks"] = "500";
dt.Rows.Add(_ravi);
Posted by: Guest on August-20-2020
1

datatable in c#

String[] arr = new String[] { "a", "b" };
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("other");
            dt.Rows.Add(arr);

            foreach (DataRow r in dt.Rows)
            {
                Console.WriteLine(r["name"].ToString() + " - " + r["other"].ToString());
            }
Posted by: Guest on October-19-2021
0

tipar datatable in dto c#

Criando a classe para converter o DataTable para List

A seguir crie a classe Conversor com o método estático ConverterParaLista():

 using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace WF_DataTable_Objetos
{
    public static class Conversor
    {
        public static List<T> ConverterParaLista<T>(DataTable dt)
        {
            var columnNames = dt.Columns.Cast<DataColumn>().Select(c => c.ColumnName.ToLower()).ToList();
            var properties = typeof(T).GetProperties();
            return dt.AsEnumerable().Select(row => {
                var objT = Activator.CreateInstance<T>();
                foreach (var pro in properties)
                {
                    if (columnNames.Contains(pro.Name.ToLower()))
                    {
                        try
                        {
                            pro.SetValue(objT, row[pro.Name]);
                        }
                        catch (Exception)
                        { }
                    }
                }
                return objT;
            }).ToList();
        }
    }
}
Posted by: Guest on November-27-2020
-2

how to make a datatable in c#

Datatable NameHere = new DataTable();
NameHere.Rows.Add("Whateveryoudliketoaddintorstringoranything");
Posted by: Guest on May-22-2020

Browse Popular Code Answers by Language