/// <summary> /// SN_Table转换为JSON /// </summary> /// <returns></returns> public string SN_TableToJson() { string result = string.Empty; if (dataGridView1.DataSource == null) { return(result); } DataTable dt = (DataTable)dataGridView1.DataSource; if (dt == null || dt.Rows.Count <= 0) { return(result); } SNConfig config = new SNConfig(); List <SN> snList = new List <SN>(); foreach (DataRow row in dt.Rows) { SN sn = new SN(); sn.Oid = row[config.Oid] == DBNull.Value ? 0 : Convert.ToInt32(row[config.Oid]); sn.Name = row[config.Name] == DBNull.Value ? "" : (string)row[config.Name]; sn.IsUsed = row[config.IsUsed] == DBNull.Value ? false : Convert.ToBoolean(row[config.IsUsed]); sn.Order = row[config.Order] == DBNull.Value ? 0 : Convert.ToInt32(row[config.Order]); sn.Code = row[config.Code] == DBNull.Value ? "" : (string)row[config.Code]; snList.Add(sn); } result = JsonConvert.SerializeObject(snList); return(result); }
/// <summary> /// SN_Json转换为Tabel /// </summary> /// <param name="snJsonnString"></param> public void SN_JsonToTable(string snJsonnString) { if (string.IsNullOrWhiteSpace(snJsonnString)) { return; } List <SN> snList = JsonConvert.DeserializeObject <List <SN> >(snJsonnString); if (snList == null || snList.Count == 0) { return; } _snTable.Clear(); SNConfig config = new SNConfig(); foreach (SN item in snList) { DataRow row = _snTable.NewRow(); row[config.Oid] = item.Oid; row[config.Name] = item.Name; row[config.IsUsed] = item.IsUsed; row[config.Order] = item.Order; row[config.Code] = item.Code; _snTable.Rows.Add(row); } bindGrid(dataGridView1, _snTable, new int[] { 0 }); }
/// <summary> /// 初始化SN /// </summary> public void InitSNTable() { SNConfig sn = new SNConfig(); _snTable = new DataTable(); _snTable.Columns.Add(sn.Oid, typeof(int)); _snTable.Columns.Add(sn.Name, typeof(string)); _snTable.Columns.Add(sn.IsUsed, typeof(bool)); _snTable.Columns.Add(sn.Order, typeof(int)); _snTable.Columns.Add(sn.Code, typeof(string)); bindGrid(dataGridView1, _snTable, new int[] { 0 }); }