zoukankan      html  css  js  c++  java
  • C# 向DataTable中插入伪造DataTable数据及实现DataTable行列转换

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Demo
    {
        public partial class FormDataTable : Form
        {
            public FormDataTable()
            {
                InitializeComponent();
                dgvOld.DataSource = InsertFakeData();
                dgvNew.DataSource = ChangeRowCol();
            }
            /// <summary>
            /// C# 向DataTable中插入伪造DataTable数据
            /// </summary>
            private DataTable InsertFakeData()
            {
                DataTable table = new DataTable("Datas");
                try
                {
                    table.Columns.Add("ID", Type.GetType("System.Int32"));
                    table.Columns[0].AutoIncrement = true;
                    table.Columns[0].AutoIncrementSeed = 1;
                    table.Columns[0].AutoIncrementStep = 1;
                    table.Columns.Add("Product", Type.GetType("System.String"));
                    table.Columns.Add("Count", Type.GetType("System.Int32"));
                    table.Columns.Add("Version", Type.GetType("System.String"));
                    table.Columns.Add("Description", Type.GetType("System.String"));
    
                    DataRow newRow;
                    newRow = table.NewRow();
                    newRow["Product"] = "水果刀";
                    newRow["Count"] = 30;
                    newRow["Version"] = "2.0";
                    newRow["Description"] = "打架专用";
                    table.Rows.Add(newRow);
    
                    //newRow = table.NewRow();
                    //newRow["Product"] = "折叠凳";
                    //newRow["Count"] = 35;
                    //newRow["Version"] = "3.0";
                    //newRow["Description"] = "行走江湖七武器之一";
                    //table.Rows.Add(newRow);
    
                }
                catch (Exception ex)
                {
                    throw new Exception("错误信息:" + ex.Message + ex.StackTrace);
                }
                return table;
               
            }
            /// <summary>
            /// 实现行列切换转换
            /// </summary>
            /// <returns></returns>
            private DataTable ChangeRowCol()
            {
                DataTable dt = InsertFakeData();
                DataTable result = new DataTable();
                result.Columns.Add("A", typeof(string));
                result.Columns.Add("B",Type.GetType("System.String"));
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        DataRow dr = result.NewRow();
                        dr[0] = dc.ColumnName;
                        if (dt.Rows[0][dc.ColumnName] == DBNull.Value)
                        {
                            dr[1] = 0;
                        }
                        else
                        {
                            dr[1] = dt.Rows[0][dc.ColumnName];
                        }
                        result.Rows.Add(dr);
                    }
                }
                else
                {
                    if (dt != null)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            DataRow dr = result.NewRow();
                            dr[0] = dc.ColumnName;
                            dr[1] = 0;
                            result.Rows.Add(dr);
                        }
                    }
                }
                return result;
            }
        }
    }

    运行结果如下图:

    清清软件园 http://sillysoft.taobao.com
  • 相关阅读:
    Leetcode Excel Sheet Column Number
    AlgorithmsI PA2: Randomized Queues and Deques Subset
    AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue
    AlgorithmsI PA2: Randomized Queues and Deques Deque
    AlgorithmsI Programming Assignment 1: PercolationStats.java
    hdu多校第四场 1003 (hdu6616) Divide the Stones 机智题
    hdu多校第四场 1007 (hdu6620) Just an Old Puzzle 逆序对
    hdu多校第四场1001 (hdu6614) AND Minimum Spanning Tree 签到
    hdu多校第三场 1007 (hdu6609) Find the answer 线段树
    hdu多校第三场 1006 (hdu6608) Fansblog Miller-Rabin素性检测
  • 原文地址:https://www.cnblogs.com/lqsilly/p/3062664.html
Copyright © 2011-2022 走看看