zoukankan      html  css  js  c++  java
  • DataGridView转datatable

      /// <summary>
            ///  datagridview转datatable
            /// </summary>
            /// <param name="dv"></param>
            /// <returns></returns>
            public static DataTable dvtodt(DataGridView dv)
            {
                DataTable dt = new DataTable();
                DataColumn dc;
                for (int i = 0; i < dv.Columns.Count; i++)
                {
                    dc = new DataColumn();
                    dc.ColumnName = dv.Columns[i].HeaderText.ToString();
                    dt.Columns.Add(dc);
                }
                for (int j = 0; j < dv.Rows.Count; j++)
                {
                    DataRow dr = dt.NewRow();
                    for (int x = 0; x < dv.Columns.Count; x++)
                    {
                        dr[x] = dv.Rows[j].Cells[x].Value;
                    }
                    dt.Rows.Add(dr);
                }
                return dt;
            }

      //private void DataGridViewToDataTable()
            //{
            //    DataTable dt = new DataTable("tablename");
            //    DataRow dr;
            //    dt.Columns.Add("商品ID", System.Type.GetType("System.Int16"));
            //    dt.Columns.Add("商品名称", System.Type.GetType("System.Int16"));
            //    dt.Columns.Add("商品性别", System.Type.GetType("System.Int16"));
            //    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            //    {
            //        dr = dt.NewRow();
            //        for (int j = 0; j <= 2; j++)
            //        {
            //            dr[j] = Convert.ToInt16(this.dataGridView1.Rows[i].Cells[j].Value);
            //        }
            //        dt.Rows.Add(dr);
            //    }
            //    DataSet selectds = new DataSet();
            //    selectds.Tables.Add(dt);
            //}

  • 相关阅读:
    学习SpringMVC——从HelloWorld开始
    线性队列
    线性表之链表
    线性表之顺序表
    nextSibling 属性与 nextElementSibling 属性的异同
    JavaScript数组增删方法总结
    class关键字
    JS三座大山_单线程&EventLoop
    JS三座大山_闭包
    JS三座大山_原型与原型链
  • 原文地址:https://www.cnblogs.com/leischen/p/2826225.html
Copyright © 2011-2022 走看看