找回密码
 立即注册

QQ登录

只需一步,快速开始

hiliuyong

初级会员

37

主题

61

帖子

270

积分

初级会员

积分
270
hiliuyong
初级会员   /  发表于:2021-8-31 00:50  /   查看:2459  /  回复:6
如题
我需要对修改过的数据保存进数据库,
没有修改过的则不处理,
遍历的方式,使用了BindingList.

6 个回复

倒序浏览
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-8-31 09:41:25
沙发
问题已收到,我验证后给您回复
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-8-31 11:18:45
板凳
本帖最后由 Richard.Ma 于 2021-8-31 11:20 编辑

BindingList中的对象如果实现了INotifyPropertyChanged接口,那么在flexgrid编辑数据后,就可以触发BindingList的ListChanged事件,然后可以给这个项目打一个Dirty的标记(比如可以加一个status属性专门存储状态)
参考下面的代码,创建了一个实现了INotifyPropertyChanged的类Product

  1. static class MyData
  2.     {

  3.         public static string[] country = new string[] { "中国", "美国", "日本" };
  4.         public static string[] product = new string[] { "苹果", "菠萝", "香蕉" };

  5.         public static BindingList<Product> GetProductList(int num)
  6.         {
  7.             BindingList<Product> list = new BindingList<Product>();
  8.             Random r = new Random();
  9.             for (int i = 0; i < num; i++)
  10.             {
  11.                 list.Add(
  12.                     new Product()
  13.                     {
  14.                         Id = i,
  15.                         Country = country[r.Next(3)],
  16.                         Name = product[r.Next(3)],
  17.                         Date = DateTime.Now.AddDays(r.Next(1000) - 1000),
  18.                         Price = r.Next(10, 100),
  19.                         Count = r.Next(1, 5),
  20.                         Status= Status.NoChange
  21.                     }
  22.                 );
  23.             }
  24.             return list;
  25.         }
  26.     }
  27.     public class Product:INotifyPropertyChanged
  28.     {
  29.         private int id;
  30.         private string country;
  31.         private string name;
  32.         private DateTime date;
  33.         private int price;
  34.         private int count;
  35.         private Status status;
  36.         public int Id { get => id; set => id = value; }
  37.         public string Country {  get => country; set {country = value;NotifyPropertyChanged("Country");}}
  38.         public string Name { get => name; set { name = value; NotifyPropertyChanged("Name"); } }
  39.         public DateTime Date { get => date; set { date = value; NotifyPropertyChanged("Date"); } }
  40.         public int Price { get => price; set { price = value; NotifyPropertyChanged("Price"); } }
  41.         public int Count { get => count; set { count = value; NotifyPropertyChanged("Count"); } }
  42.         public Status Status { get => status; set => status = value; }

  43.         public event PropertyChangedEventHandler PropertyChanged;
  44.         protected virtual void NotifyPropertyChanged(string propertyName)
  45.         {
  46.             if (PropertyChanged != null)
  47.             {
  48.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  49.             }
  50.         }
  51.     }
  52.     public enum Status
  53.     {
  54.         Modified,
  55.         Added,
  56.         Removed,
  57.         NoChange
  58.     }
复制代码

然后在Forms中的BindingList的ListChanged 事件中,

  1. BindingList<Product> list = MyData.GetProductList(10);
  2.         public Form1()
  3.         {
  4.             InitializeComponent();
  5.             
  6.             c1FlexGrid1.DataSource = list;
  7.             
  8.             list.ListChanged += List_ListChanged;
  9.         }

  10.         private void List_ListChanged(object sender, ListChangedEventArgs e)
  11.         {
  12.             if(e.ListChangedType== ListChangedType.ItemChanged)
  13.             {
  14.                 list[e.NewIndex].Status = Status.Modified;
  15.             }
  16.             else if (e.ListChangedType == ListChangedType.ItemAdded)
  17.             {
  18.                 list[e.NewIndex].Status = Status.Added;
  19.             }
  20.             else if (e.ListChangedType == ListChangedType.ItemDeleted)
  21.             {
  22.                 list[e.NewIndex].Status = Status.Removed;
  23.             }
  24.         }
复制代码



回复 使用道具 举报
hiliuyong
初级会员   /  发表于:2021-9-2 19:04:49
地板
这个问题我想了一个办法可以简化,
因为这个是绑定,我可以只改变C1Flexgrid上的数据,然后绑定的数据就自动改变了,
那么问题来了,我能够直接判断表格的每行数据是否是新建、修改、删除的属性吗,也就是行的Status ,例如 C1Flexgrid.Row[5].Status = Status.Modified
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-9-2 23:56:02
5#
目前没有提供类似的功能
回复 使用道具 举报
hiliuyong
初级会员   /  发表于:2021-9-3 16:08:23
6#
你说的是对的,是否修改只能放在数据集里面,不太合适放在表格上。
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-9-3 17:24:43
7#
嗯,所以flexgrid目前没有提供此类功能
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部