using DevExpress.Data; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid; using Gs.DevApp.ToolBox; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Windows.Forms; namespace Gs.DevApp.UserControl { // 继承自XtraForm,这是DevExpress提供的增强型表单控件 public partial class SelectCgMx : XtraForm { // Web服务名称常量,用于调用后端接口 private readonly string _webServiceName = "MesInvItemArnManager/"; // 供应商ID和收料组织ID的私有变量 private string suppId = ""; private string receiveOrgId = ""; /// /// 构造函数 /// /// 供应商ID参数 /// 收料组织ID参数 public SelectCgMx(string _suppId, string _receiveOrgId) { // 初始化表单控件 InitializeComponent(); /* #region 自动汇总beg gridView1.Columns["purchaseQty"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom; gridView1.Columns["yssl"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom; gridView1.Columns["wssl"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom; gridView1.CustomSummaryCalculate += (sender, e) => { GridView view = sender as GridView; if (e.SummaryProcess == CustomSummaryProcess.Start) { // 初始化汇总值 e.TotalValue = 0m; } else if (e.SummaryProcess == CustomSummaryProcess.Calculate) { // 检查当前行的Status字段是否为1 int status = Convert.ToInt32(view.GetListSourceRowCellValue(e.RowHandle, "chkInt")); if (status == 1) { string _colCaption = e.Item.ToString(); if (_colCaption.Contains("采购")) { decimal amount = Convert.ToDecimal(view.GetListSourceRowCellValue(e.RowHandle, "purchaseQty")); e.TotalValue = Convert.ToDecimal(e.TotalValue) + amount; } else if (_colCaption.Contains("已收")) { decimal amount2 = Convert.ToDecimal(view.GetListSourceRowCellValue(e.RowHandle, "yssl")); e.TotalValue = Convert.ToDecimal(e.TotalValue) + amount2; } else if (_colCaption.Contains("未收")) { decimal amount3 = Convert.ToDecimal(view.GetListSourceRowCellValue(e.RowHandle, "wssl")); e.TotalValue = Convert.ToDecimal(e.TotalValue) + amount3; } } } else if (e.SummaryProcess == CustomSummaryProcess.Finalize) { // 设置最终汇总值 e.TotalValue = e.TotalValue; } }; // 添加复选框变化事件,实时更新汇总 gridView1.CellValueChanged += (s, e) => { if (e.Column.FieldName == "chkInt") { // 复选框值变化时强制刷新汇总 gridView1.UpdateSummary(); } }; #endregion */ // 为GridView的列标题绘制事件添加处理方法,使用自定义绘制 this.gridView1.CustomDrawColumnHeader += (s, e) => { Gs.DevApp.ToolBox.UtilityHelper.CustomDrawColumnHeader(s, e); }; // 添加鼠标点击事件处理复选框点击 this.gridView1.MouseUp += (s, e) => { Gs.DevApp.ToolBox.UtilityHelper.CustomMouseUp(s, e, gcMain, gridView1); }; // 配置复选列的排序和筛选选项,禁用排序和筛选功能 this.colChkInt.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False; this.colChkInt.OptionsFilter.AllowAutoFilter = false; this.colChkInt.OptionsFilter.AllowFilter = false; this.colChkInt.OptionsFilter.AllowInHeaderSearch = DevExpress.Utils.DefaultBoolean.False; // 设置单选按钮组默认选中第一项(采购类型) radioGroup1.SelectedIndex = 0; // 保存传入的供应商ID和收料组织ID this.suppId = _suppId; this.receiveOrgId = _receiveOrgId; // 设置GridView的参数配置 Gs.DevApp.ToolBox.UtilityHelper.SetGridViewParameter(gridView1, null, null, null, "", null, null, false); // 加载第一页数据 getPageList(1); // 为"确定"按钮添加点击事件处理 btnIn.Click += (s, e) => { // 提交编辑并更新当前行数据 gridView1.PostEditor(); gridView1.UpdateCurrentRow(); // 收集选中行的GUID var list = new List(); DataTable dt = this.gcMain.DataSource as DataTable; { foreach (DataRow dr in dt.Rows) { string checkBox = dr["chkInt"].ToString(); string _guid = dr["guid"].ToString(); // 检查是否选中 if (Gs.DevApp.ToolBox.UtilityHelper.ToCheck(checkBox)) { list.Add(_guid); } } } // 触发回调事件,传递选中的GUID列表,然后关闭表单 UpdateParent?.Invoke(this, new UpdateParentEventArgs { StringList = list }); Close(); }; // 为单选按钮组添加选中项变化事件,切换时重新加载数据 radioGroup1.SelectedIndexChanged += (s, e) => { getPageList(1); }; } /// /// 选择后的回调事件,用于向父窗体传递选中的数据 /// public event EventHandler UpdateParent; /// /// 从服务器获取分页数据并绑定到GridView /// /// 当前页码 private void getPageList(int curPage) { // 创建请求参数对象 var _obj = new { currentPage = curPage, // 当前页码 everyPageSize = 999999, // 每页记录数(这里设置了一个很大的值,可能是为了获取所有数据) sortName = "", // 排序字段 keyWhere = "", // 查询条件 inBusType = (radioGroup1.SelectedIndex + 1), // 业务类型:1是采购,2是委外 inSupId = this.suppId, // 供应商ID inReceiveOrgId = this.receiveOrgId, // 收料组织ID }; // 将参数对象序列化为JSON字符串 var json = JsonConvert.SerializeObject(_obj); try { // 调用Web服务获取数据 var strReturn = UtilityHelper.HttpPost("", _webServiceName + "SelectForm", json); // 解析返回结果为DataTable var dd = UtilityHelper.ReturnToTablePage(strReturn); var dt = dd.rtnData.list; // 绑定数据到GridControl gcMain.BindingContext = new BindingContext(); gcMain.DataSource = dt; gcMain.ForceInitialize(); // 自动调整列宽并应用网格布局 gridView1.BestFitColumns(); Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gridView1); } catch (Exception ex) { // 显示错误信息 MsgHelper.Warning("提示:" + ex.Message); } } } }