C# Datatable的使用方法

来源:互联网 发布:淘宝买东西 编辑:程序博客网 时间:2024/06/06 01:47
        private void Func()
        {

   DataTable _dt=null;

            if (_dt != null)
            {
                _dt = null;
            }
            _dt = new DataTable();
            DataRow dr = null;
            _dt.Columns.Add("商品", typeof(string));
            _dt.Columns.Add("售价", typeof(double));
            _dt.Columns[0].Caption = "System.String";
            _dt.Columns[0].DataType = typeof(string);
            _dt.Columns[1].Caption = "System.Double";
            _dt.Columns[1].DataType = typeof(double);
            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                if (dr == null)
                {
                    dr = _dt.NewRow();
                }
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    dr[j] = dataGridView1.Rows[i].Cells[j].Value;
                }
                _dt.Rows.Add(dr);
                dr = null;
            }
        }
0 0
原创粉丝点击