FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 12509|回复: 10
打印 上一主题 下一主题

编辑单元格:保存真实数据库

[复制链接]
跳转到指定楼层
楼主
发表于 2015-1-23 15:03:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 shouzhi2007 于 2015-1-23 15:07 编辑

本人写一下这两天调试的结果,哎,说实在也有点菜了,还问了好几位大神在帮助,最终还是大神⑥阿太⑥帮助完成了。

我写这个是为了和例子没有数据库的操作。本人之前在论坛上找了几久没有相应的结果,所以把心得写下来,为大家服务一下。(大神略过,指点!)

首先:
参考例子:http://fineui.com/demo/#/demo/grid/grid_editor_cell_databind.aspx
  1. //保存更新代码,
  2. protected void Button1_Click(object sender, EventArgs e)
  3.         {
  4.             Dictionary<int, Dictionary<string, object>> modifiedDict = Grid3.GetModifiedDict();

  5.             foreach (int rowIndex in modifiedDict.Keys)
  6.             {
  7.                 int rowID = Convert.ToInt32(Grid3.DataKeys[rowIndex][0]);

  8.                 //这一行为从数据库根据 rowID读数据
  9.                 EquipmentLog item = DB.EquipmentLogs.Where(u => u.ID == rowID).FirstOrDefault();
  10.                 //赋予ID主键
  11.                 item.ID = rowID;

  12.                 //DataRow row = FindRowByID(rowID);
  13.               
  14.                 UpdateDataRow(modifiedDict[rowIndex], item);

  15.                 //保存
  16.                DB.SaveChanges();
  17.                
  18.             }

  19.             BindGrid();

  20.              Label3.Text = "用户修改的数据:" + Grid3.GetModifiedData().ToString(Newtonsoft.Json.Formatting.None);

  21.             Alert.Show("数据保存成功!(表格数据已重新绑定)");
  22.         }
复制代码
  1. //更新代码
  2.    private static void UpdateDataRow(Dictionary<string, object> rowDict, EquipmentLog rowData)
  3.         {
  4.             if (rowDict.ContainsKey("x1"))
  5.             {
  6.                 rowData.x1 = rowDict["x1"].ToString();

  7.             }
  8.             if (rowDict.ContainsKey("x2"))
  9.             {
  10.                 rowData.x2 = rowDict["x2"].ToString();
  11.             }
  12.             if (rowDict.ContainsKey("x3"))
  13.             {
  14.                 rowData.x3 = rowDict["x3"].ToString();
  15.             }
  16.             if (rowDict.ContainsKey("x4"))
  17.             {
  18.                 rowData.x4 = rowDict["x4"].ToString();
  19.             }
  20.            

  21.         }
复制代码

实体库:EquipmentLog

沙发
 楼主| 发表于 2015-1-23 15:03:57 | 只看该作者
  1. //下拉列表代码
  2.         private void BindDropDownList()
  3.         {
  4.             List<string> majors = new List<string>();
  5.             majors.Add("Δ");
  6.             majors.Add("★");
  7.             majors.Add("√");
  8.             majors.Add("x");

  9.             ddlMajor.DataSource = majors;
  10.             ddlMajor.DataBind();

  11.             DropDownList1.DataSource = majors;
  12.             DropDownList1.DataBind();
  13.             DropDownList2.DataSource = majors;
  14.             DropDownList2.DataBind();
  15.             DropDownList3.DataSource = majors;
  16.             DropDownList3.DataBind();
  17.          
  18.         }  
复制代码
板凳
 楼主| 发表于 2015-1-23 15:04:14 | 只看该作者
  1. //  获取数据源代码  
  2.         private DataTable GetSourceData()
  3.         {

  4.             IQueryable<EquipmentLog> q = DB.EquipmentLogs;

  5.             DataTable dt = LINQToDataTable<EquipmentLog>(q);


  6.             return dt;
  7.         }
复制代码
地板
 楼主| 发表于 2015-1-23 15:04:32 | 只看该作者
  1. //将IEnumerable<T>类型的集合转换为DataTable类型
  2.         public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
  3.         {   //定义要返回的DataTable对象
  4.             DataTable dtReturn = new DataTable();
  5.             // 保存列集合的属性信息数组
  6.             PropertyInfo[] oProps = null;
  7.             if (varlist == null) return dtReturn;//安全性检查
  8.             //循环遍历集合,使用反射获取类型的属性信息
  9.             foreach (T rec in varlist)
  10.             {
  11.                 //使用反射获取T类型的属性信息,返回一个PropertyInfo类型的集合
  12.                 if (oProps == null)
  13.                 {
  14.                     oProps = ((Type)rec.GetType()).GetProperties();
  15.                     //循环PropertyInfo数组
  16.                     foreach (PropertyInfo pi in oProps)
  17.                     {
  18.                         Type colType = pi.PropertyType;//得到属性的类型
  19.                         //如果属性为泛型类型
  20.                         if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
  21.                         == typeof(Nullable<>)))
  22.                         {   //获取泛型类型的参数
  23.                             colType = colType.GetGenericArguments()[0];
  24.                         }
  25.                         //将类型的属性名称与属性类型作为DataTable的列数据
  26.                         dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
  27.                     }
  28.                 }
  29.                 //新建一个用于添加到DataTable中的DataRow对象
  30.                 DataRow dr = dtReturn.NewRow();
  31.                 //循环遍历属性集合
  32.                 foreach (PropertyInfo pi in oProps)
  33.                 {   //为DataRow中的指定列赋值
  34.                     dr[pi.Name] = pi.GetValue(rec, null) == null ?
  35.                         DBNull.Value : pi.GetValue(rec, null);
  36.                 }
  37.                 //将具有结果值的DataRow添加到DataTable集合中
  38.                 dtReturn.Rows.Add(dr);
  39.             }
  40.             return dtReturn;//返回DataTable对象
  41.         }
复制代码
5#
 楼主| 发表于 2015-1-23 15:06:16 | 只看该作者

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
6#
发表于 2015-3-19 09:59:35 | 只看该作者
不错,赞一个
7#
发表于 2015-3-20 10:46:56 | 只看该作者
小小的建议:
  1. List<string> majors = new List<string>();
  2.             majors.Add("Δ");
  3.             majors.Add("★");
  4.             majors.Add("√");
  5.             majors.Add("x");
复制代码
换成
  1. List<string> majors = new List<string>(){"Δ","★","√","x"};
复制代码
至少能减少一点代码行数,看上去也舒服一点
8#
 楼主| 发表于 2015-3-24 11:50:28 | 只看该作者
谢谢!                                
9#
发表于 2015-3-30 17:26:49 | 只看该作者
不错,学习了{:soso_e179:}
10#
发表于 2016-10-10 18:24:29 | 只看该作者
lz你好 , 我将数据显示到 Grid 中,修改后, 调用      
Label1.Text = String.Format("用户修改的数据:<pre>{0}</pre>", Grid1.GetModifiedData().ToString(Newtonsoft.Json.Formatting.Indented));
没有显示任何修改的值,可能是有哪些原因?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2024-5-3 07:36 , Processed in 0.051860 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表