using SelfHelpClient.BL; using SelfHelpClient.BO; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace SelfHelpClient { public partial class WeightForm : Form { WeightBill entity; Thread refersh; public WeightForm(ViewEntity view) { InitializeComponent(); if (view.BillType == 0) entity = WeightBillBL.CreateWeightBill(view.ID); else entity = WeightBillBL.GetWeightBill(view.ID); var tableHeight = InitTableLayoutPanel(); BuildGridPanel(); this.FormClosing += delegate { if (refersh != null && refersh.IsAlive) refersh.Abort(); }; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); farmerGrid.DataSource = entity.FarmerDetails; farmerGrid.Refresh(); weightGrid.DataSource = entity.Details; weightGrid.Refresh(); refersh = new Thread(RefershDetail); refersh.Start(); } private void RefershDetail() { while (true) { Thread.Sleep(2000); if (this.IsHandleCreated) { this.Invoke(new Action(() => { var detail = WeightBillBL.GetWeightDetail(entity.ID); if (detail.Any()) { weightGrid.DataSource = detail; weightGrid.Refresh(); } })); } } } private void BuildGridPanel() { panel5.Location = new Point(6, panel2.Location.Y + panel2.Height + 6); panel5.Height = 320; farmerGrid.Height = 180; linePanel.Location = new Point(0, 9 + 180 + 15); weightGrid.Height = 110; weightGrid.Location = new Point(9, linePanel.Location.Y + 15); } Color fontColor = Color.FromArgb(238, 104, 15); private int InitTableLayoutPanel() { TableLayoutPanel tablePanel = new TableLayoutPanel(); tablePanel.Location = new Point(9, 15); tablePanel.Width = Screen.PrimaryScreen.Bounds.Width - 10; var dic = BuildFields(); var table = ComputeColAndRow(dic.Count); tablePanel.Height = 40 * table.Rows + 20; panel2.Height = tablePanel.Height + 10; panel2.Controls.Add(tablePanel); DynamicLayout(tablePanel, table); var type = typeof(WeightBill); var r = 0; var c = 0; foreach (var item in dic) { Label title = BuildLabel(item.Value + ":"); tablePanel.Controls.Add(title); tablePanel.SetRow(title, r); tablePanel.SetColumn(title, c); c++; var v = type.GetProperty(item.Key).GetValue(entity); var lbl = string.Empty; switch (item.Key) { case "WeighTime": lbl = string.Format("{0:yyyy-MM-dd HH:mm:ss}", v); break; case "JingJianFee": case "DiscontMoney": case "ShackWeight": case "ShackPrice": case "ShackMoney": lbl = string.Format("{0:#0.######}", v); break; case "AnimalTestDate": lbl = string.Format("{0:yyyy-MM-dd}", v); break; default: lbl = string.Format("{0}", v); break; } Label content = BuildLabel(lbl); content.ForeColor = fontColor; tablePanel.Controls.Add(content); tablePanel.SetRow(content, r); tablePanel.SetColumn(content, c); c++; if (c % table.Cols == 0) { c = 0; r++; } } return tablePanel.Height; } TableSturct ComputeColAndRow(int fileCount) { var screenWidth = Screen.PrimaryScreen.Bounds.Width - 10; var col = Math.Floor((decimal)screenWidth / (110 + 200)); var row = Math.Ceiling(fileCount / col); return new TableSturct() { Rows = (int)row, Cols = (int)col * 2 }; } private void DynamicLayout(TableLayoutPanel layoutPanel, TableSturct table) { layoutPanel.RowCount = table.Rows; //设置分成几行 for (int i = 0; i < table.Rows; i++) { layoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 40)); } layoutPanel.ColumnCount = table.Cols; //设置分成几列 for (int i = 0; i < table.Cols; i++) { var precent = i % 2 == 0 ? 30F : 70F; layoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, precent)); } } Dictionary BuildFields() { var dic = new Dictionary() { {"Supplier_Name","供应商"},{"BankAccount","银行卡号"},{"PurchaseType_Name","收购类型"},{"WeighTime","过磅时间"},{"Car_Name","车辆"},{"Employee_Name","业务员"},{"JingJianFee","经检费"},{"DiscontMoney","猪场扣款"},{"ShackWeight","棚前重量"},{"ShackPrice","棚前单价"},{"ShackMoney","棚前金额"},{"AnimalTestMan","动检人"},{"AnimalTestNumber","动检证号"},{"AnimalTestDate","动检日期"}, }; return dic; } Label BuildLabel(string content) { Label label = new Label(); label.Text = content; label.Font = new System.Drawing.Font("黑体", 12); label.Dock = DockStyle.Fill; label.TextAlign = ContentAlignment.MiddleLeft; return label; } private void backBtn_Click(object sender, EventArgs e) { MainForm.Form.Show(); this.Close(); } private void okBtn_Click(object sender, EventArgs e) { //目前过磅信息界面上的确认按钮没有功能,暂时做点击完后返回主界面; MainForm.Form.Show(); this.Close(); } Pen penSelected = new Pen(Color.FromArgb(215, 218, 243)); private void farmerGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { var rec = e.RowBounds; Point pTopStart = new Point(rec.X, rec.Y + 1); Point pTopEnd = new Point(rec.X + rec.Width, rec.Y + 1); e.Graphics.DrawLine(penSelected, pTopStart, pTopEnd); Point pLeftStart = new Point(rec.X + 1, rec.Y); Point pLeftEnd = new Point(rec.X + 1, rec.Y + rec.Height); e.Graphics.DrawLine(penSelected, pLeftStart, pLeftEnd); Point pRightStart = new Point(rec.X + rec.Width - 1, rec.Y); Point pRightEnd = new Point(rec.X + rec.Width - 1, rec.Y + rec.Height); e.Graphics.DrawLine(penSelected, pRightStart, pRightEnd); } private void weightGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { var rec = e.RowBounds; Point pTopStart = new Point(rec.X, rec.Y + 1); Point pTopEnd = new Point(rec.X + rec.Width, rec.Y + 1); e.Graphics.DrawLine(penSelected, pTopStart, pTopEnd); Point pLeftStart = new Point(rec.X + 1, rec.Y); Point pLeftEnd = new Point(rec.X + 1, rec.Y + rec.Height); e.Graphics.DrawLine(penSelected, pLeftStart, pLeftEnd); Point pRightStart = new Point(rec.X + rec.Width - 1, rec.Y); Point pRightEnd = new Point(rec.X + rec.Width - 1, rec.Y + rec.Height); e.Graphics.DrawLine(penSelected, pRightStart, pRightEnd); } } struct TableSturct { public int Rows { get; set; } public int Cols { get; set; } } }