lixiaojun
2024-12-19 1dd158434a41627a6684cd630b2696fb83b1e3d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using DevExpress.XtraEditors;
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 HStation.WinFrmUI
{
    public partial class AssetsMeterSingleMatchingCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public AssetsMeterSingleMatchingCtrl()
        {
            InitializeComponent();
            this.layoutControl1.SetupLayoutControl();
            this.gridView1.SetLimitView();
            this.gridView1.RegistCustomDrawCell();
            this.generalSearchCtrl1.SearchEvent += Search;
            this.generalSearchCtrl1.ClearEvent += Clear;
        }
 
        public List<AssetsMeterSingleMatchingViewModel> _allList = null;
        private List<AssetsMeterSingleMatchingViewModel> _allBindingList = null;
        private AssetsMeterMainVmo _selected = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public async void SetBindingData(string dbId)
        {
            var allList = await BLLFactory<HStation.BLL.AssetsMeterMain>.Instance.GetAll();
            _allList = new List<AssetsMeterSingleMatchingViewModel>();
            allList.ForEach(x => _allList.Add(new AssetsMeterSingleMatchingViewModel(x)));
            if (long.TryParse(dbId, out long id))
            {
                var item = allList?.Find(x => x.ID == id);
                _selected = item;
            }
            Search();
            if (_selected != null)
            {
                if (_allBindingList != null && _allBindingList.Count > 0)
                {
                    await Task.Run(() =>
                     {
                         var dataSourceIndex = _allBindingList.FindIndex(x => x.Vmo == _selected);
                         if (dataSourceIndex >= 0)
                         {
                             var rowIndex = this.gridView1.GetRowHandle(dataSourceIndex);
                             this.gridView1.FocusedRowHandle = rowIndex;
                         }
                     });
                }
            }
        }
 
        private void Search()
        {
            _allBindingList = _allList;
            var name = this.txtName.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                _allBindingList = _allBindingList.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.Contains(name)).ToList();
            }
            this.assetsMeterSingleMatchingViewModelBindingSource.DataSource = _allBindingList;
            this.assetsMeterSingleMatchingViewModelBindingSource.ResetBindings(false);
        }
 
        private void Clear()
        {
            this.txtName.EditValue = null;
            Search();
        }
 
        /// <summary>
        /// 获取
        /// </summary>
        public AssetsMeterMainVmo Get()
        {
            var vm = this.gridView1.GetFocusedRow() as AssetsMeterSingleMatchingViewModel;
            if (vm == null)
            {
                return default;
            }
            return vm.Vmo;
        }
 
    }
}