C# DataTable 转 json

   public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //窗体加载事件  
            dataGridView1.DataSource = getData2();
            DataTable table = getData2();
            textBox1.Text = DataTableToJson(getData2()) + "\r\n";
            textBox1.Text += DataTableToJson(table) + "\r\n";

        }


        #region 获得 DataTable数据 
        public DataTable getData1()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("编号", typeof(Int32));
            dt.Columns.Add("姓名", typeof(string));
            dt.Columns.Add("性别", typeof(string));
            dt.Columns.Add("学历", typeof(string));
            dt.Rows.Add(1, "王超", "", "本科");
            dt.Rows.Add(2, "周丽", "", "专科");
            dt.Rows.Add(3, "李娟", "", "专科");
            dt.Rows.Add(4, "杨明", "", "硕士");
            dt.Rows.Add(5, "张德", "", "本科");
            return dt;
        }
        public DataTable getData2()  
        {
            DataTable dt = new DataTable("Student");
            dt.Columns.Add("StudentId", typeof(Int32));
            dt.Columns.Add("StudentName", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            dt.Columns.Add("MobileNo", typeof(string));
            //Data  
            dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000");
            dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
            dt.Rows.Add(3, "Namit", "Pune", "1222222222");
            dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
            return dt;
        }
        #endregion

        #region 从DataTable数据转为List
        public List<Student> StudentList()
        {
            DataTable dt = new DataTable();
            dt = getData2();
            List<Student> studentList = new List<Student>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Student student = new Student();
                student.StudentId = Convert.ToInt32(dt.Rows[i]["StudentId"]);
                student.StudentName = dt.Rows[i]["StudentName"].ToString();
                student.Address = dt.Rows[i]["Address"].ToString();
                student.MobileNo = dt.Rows[i]["MobileNo"].ToString();
                studentList.Add(student);
            }
            return studentList;
        }
        #endregion

        #region 从DataTable数据 转为 Json
        public string DataTableToJson(DataTable table)
        {
            var JsonString = new StringBuilder();
            if (table.Rows.Count > 0)
            {
                JsonString.Append("[");
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    JsonString.Append("{");
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        if (j < table.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == table.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
                        }
                    }
                    if (i == table.Rows.Count - 1)
                    {
                        JsonString.Append("}");
                    }
                    else
                    {
                        JsonString.Append("},");
                    }
                }
                JsonString.Append("]");
            }
            return JsonString.ToString();
        }
        #endregion
    }
    #region 实体类
    public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public string Address { get; set; }
        public string MobileNo { get; set; }
    }

    #endregion



创建datatable

dt = new DataTable();
dt.Columns.Add("a");
dt.Columns.Add("b");
dt.Rows.Add(1);
object[] iis = new object[2];
iis[0] = 1;
iis[1] = 2;
dt.Rows.Add(iis);
DataSource = dt;

 

 

private void button3_Click(object sender, EventArgs e)
        {
            //创建一个空表
            DataTable dt = new DataTable();
            dt.Columns.Add("列1");
            dt.Columns.Add("列2");
            dt.Columns.Add("列3");
            dt.Columns.Add("列4");

            for (int i=0;
                i<5;i++)
            {
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                dt.Rows[0]["列1"] = "第一列数据";//通过名称赋值
                dt.Rows[0]["列2"] = "第二列数据";//通过名称赋值
                dt.Rows[0]["列3"] = "第三列数据";//通过名称赋值
                dt.Rows[0]["列4"] = "第四列数据";//通过名称赋值
            }       
            
        }

 //插入数据到指定行

  dt = dal字典.SelectBy字典类型("产品类型");
                //产品类型.SelectedIndex = -1;
                DataRow dr = dt.NewRow();
                dr["字典类型"] = "产品类型";
                dr["字典值"] = "";
                //dt.Rows.Add(dr);
                dt.Rows.InsertAt(dr, 0);

 

posted @ 2018-03-07 15:11  enych  阅读(3712)  评论(0编辑  收藏  举报