123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Data.Linq.Mapping;
- using System.Drawing;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PTMIBase.Entity
- {
-
-
-
-
-
-
-
- public class EntityBase
- {
-
-
-
-
-
-
- protected void AddDGVColumn(DataGridView dgv, string headerText, string dataPropertyName, int width = 120,bool isAdapter = false)
- {
- DataGridViewColumn newColumn = new DataGridViewTextBoxColumn();
- newColumn.HeaderText = headerText;
- newColumn.Width = width;
- newColumn.DataPropertyName = dataPropertyName;
- newColumn.Name = dataPropertyName;
- if (isAdapter) newColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
- dgv.Columns.Add(newColumn);
-
- }
- public void SetHeaderText(DataGridView dgv,Font font)
- {
- Type type = this.GetType();
- var properties = type.GetProperties();
- dgv.AutoGenerateColumns = false;
- dgv.Columns.Clear();
- dgv.Font = font;
- foreach (var property in properties)
- {
-
- var display = property.GetCustomAttribute<DisplayNameAttribute>();
- if (display == null) continue;
- string dispalyName = display.DisplayName;
- int disLen = CalculateVarCharLength(dispalyName);
- var strLength = property.GetCustomAttribute<StringLengthAttribute>();
- if (strLength == null) continue;
- int len = strLength.MaximumLength;
- int width = len>disLen ? (int)(len*6.5f) : (int)(disLen * 6.5f);
- bool isAdapter = width > 200;
-
- AddDGVColumn(dgv, dispalyName, property.Name, width,isAdapter);
- }
- dgv.CellValueChanged += DataGridView_CellValueChanged;
- }
-
-
-
-
-
- public static int CalculateVarCharLength(string input)
- {
-
- Encoding utf16Encoding = Encoding.UTF8;
- byte[] bytes = utf16Encoding.GetBytes(input);
- return bytes.Length;
- }
-
-
-
- public JObject ConvertToDBJson()
- {
- Type type = this.GetType();
- var properties = type.GetProperties();
- JObject joRtn = new JObject();
- foreach (var property in properties)
- {
- var attribute = property.GetCustomAttribute<ColumnAttribute>();
- if (attribute == null) continue;
- string fieldName = attribute.Name;
-
- object value = property.GetValue(this, null);
-
- JToken token = value == null ? JValue.CreateNull() : JToken.FromObject(value);
- joRtn.Add(fieldName, token);
- }
- return joRtn;
- }
- private static void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
-
- if (e.RowIndex == -1 && e.ColumnIndex == -1)
- {
- AdjustColumnWidths((DataGridView)sender);
- }
- }
- private static void AdjustColumnWidths(DataGridView dataGridView)
- {
- for (int i = 0; i < dataGridView.Columns.Count; i++)
- {
- DataGridViewColumn column = dataGridView.Columns[i];
-
- float titleSize = CalculateVarCharLength(column.HeaderText);
- float maxWidth = titleSize;
-
- foreach (DataGridViewRow row in dataGridView.Rows)
- {
- if (!row.IsNewRow && !string.IsNullOrEmpty(row.Cells[i].Value?.ToString()))
- {
- float cellSize = CalculateVarCharLength(row.Cells[i].Value.ToString());
- maxWidth = Math.Max(maxWidth, cellSize);
- }
- }
-
- column.Width = (int)Math.Ceiling(maxWidth) + 5;
- }
- }
- }
- }
|