How do you convert a DataTable into a generic list?

前端 未结 27 2899
后悔当初
后悔当初 2020-11-22 17:04

Currently, I\'m using:

DataTable dt = CreateDataTableInSomeWay();

List list = new List(); 
foreach (DataRow dr in dt.Rows)
{
          


        
27条回答
  •  臣服心动
    2020-11-22 17:37

    Output

    public class ModelUser
    {
        #region Model
    
        private string _username;
        private string _userpassword;
        private string _useremail;
        private int _userid;
    
        /// 
        /// 
        /// 
        public int userid
        {
            set { _userid = value; }
            get { return _userid; }
        }
    
    
        /// 
        /// 
        /// 
    
        public string username
        {
            set { _username = value; }
            get { return _username; }
        }
    
        /// 
        /// 
        /// 
        public string useremail
        {
            set { _useremail = value; }
            get { return _useremail; }
        }
    
        /// 
        /// 
        /// 
        public string userpassword
        {
            set { _userpassword = value; }
            get { return _userpassword; }
        }
        #endregion Model
    }
    
    public List DataTableToList(DataTable dt)
    {
        List modelList = new List();
        int rowsCount = dt.Rows.Count;
        if (rowsCount > 0)
        {
            ModelUser model;
            for (int n = 0; n < rowsCount; n++)
            {
                model = new ModelUser();
    
                model.userid = (int)dt.Rows[n]["userid"];
                model.username = dt.Rows[n]["username"].ToString();
                model.useremail = dt.Rows[n]["useremail"].ToString();
                model.userpassword = dt.Rows[n]["userpassword"].ToString();
    
                modelList.Add(model);
            }
        }
        return modelList;
    }
    
    static DataTable GetTable()
    {
        // Here we create a DataTable with four columns.
        DataTable table = new DataTable();
        table.Columns.Add("userid", typeof(int));
        table.Columns.Add("username", typeof(string));
        table.Columns.Add("useremail", typeof(string));
        table.Columns.Add("userpassword", typeof(string));
    
        // Here we add five DataRows.
        table.Rows.Add(25, "Jame", "Jame@hotmail.com", DateTime.Now.ToString());
        table.Rows.Add(50, "luci", "luci@hotmail.com", DateTime.Now.ToString());
        table.Rows.Add(10, "Andrey", "Andrey@hotmail.com", DateTime.Now.ToString());
        table.Rows.Add(21, "Michael", "Michael@hotmail.com", DateTime.Now.ToString());
        table.Rows.Add(100, "Steven", "Steven@hotmail.com", DateTime.Now.ToString());
        return table;
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        List userList = new List();
    
        DataTable dt = GetTable();
    
        userList = DataTableToList(dt);
    
        gv.DataSource = userList;
        gv.DataBind();
    }[enter image description here][1]
    

    
    

提交回复
热议问题