在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Collections; namespace MyCommanHelper { public class ControlStyleHelper { #region 字段变量 // Fields public static int currentCol = -1; public static bool sort; #endregion #region 网格视图 /// <summary> /// 初始化网格视图样式 /// </summary> /// <param name="dataGridView1">网格控件</param> /// <param name="isReadOnly">是否只读</param> /// <param name="isForbidSortColumn">是否禁止排序</param> public static void InitDataGridView(DataGridView dataGridView1, bool isReadOnly, bool isForbidSortColumn) { dataGridView1.RowHeadersDefaultCellStyle.BackColor = SystemColors.GradientActiveCaption; dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.GradientActiveCaption; dataGridView1.BackgroundColor = Color.White; DataGridViewCellStyle style = new DataGridViewCellStyle(); style.SelectionBackColor = Color.FromArgb(0x80, 0x80, 0xff); style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.DefaultCellStyle = style; dataGridView1.ClearSelection(); dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = isReadOnly; if (isForbidSortColumn) { for (int i = 0; i < dataGridView1.Columns.Count; i++) { dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } } } #endregion #region 对话框 /// <summary> /// 初始化对话框模式样式 /// </summary> /// <param name="frm"></param> public static void InitFormShowDialogStyle(Form frm) { frm.ShowInTaskbar = false; frm.MaximizeBox = false; frm.MinimizeBox = false; frm.ShowIcon = false; frm.StartPosition = FormStartPosition.CenterScreen; frm.FormBorderStyle = FormBorderStyle.FixedDialog; frm.ShowDialog(); } /// <summary> /// 初始化对话框非模式样式 /// </summary> /// <param name="frm"></param> public static void InitFormShowStyle(Form frm) { frm.ShowInTaskbar = true; frm.MaximizeBox = false; frm.MinimizeBox = false; frm.ShowIcon = false; frm.StartPosition = FormStartPosition.CenterScreen; frm.FormBorderStyle = FormBorderStyle.FixedDialog; frm.Show(); } #endregion #region 列表视图 /// <summary> /// 初始化列表视图样式 /// </summary> /// <param name="listView1">列表视图控件</param> /// <param name="isCheckBox">是否显示复选框</param> public static void InitListViewStyle(ListView listView1, bool isCheckBox) { listView1.View = View.Details; listView1.ShowItemToolTips = true; listView1.GridLines = true; listView1.FullRowSelect = true; listView1.CheckBoxes = isCheckBox; } /// <summary> /// 对列排序并标示标识 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void SortListViewColumn(object sender, ColumnClickEventArgs e) { string str3; char ch = '▼'; string str = ch.ToString().PadLeft(4, ' '); ch = '▲'; string str2 = ch.ToString().PadLeft(4, ' '); ListView view = sender as ListView; if (!sort) { sort = true; str3 = view.Columns[e.Column].Text.TrimEnd(new char[] { '▼', '▲', ' ' }); view.Columns[e.Column].Text = str3 + str2; } else if (sort) { sort = false; str3 = view.Columns[e.Column].Text.TrimEnd(new char[] { '▼', '▲', ' ' }); view.Columns[e.Column].Text = str3 + str; } if (e.Column == (view.ListViewItemSorter as ListViewColumnSorter).SortColumn) { if ((view.ListViewItemSorter as ListViewColumnSorter).Order == SortOrder.Ascending) { (view.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Descending; } else { (view.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending; } } else { (view.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column; (view.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending; } ((ListView)sender).Sort(); } /// <summary> /// 内部类 /// Nested Types /// </summary> public class ListViewColumnSorter : IComparer { #region 变量 // Fields private int ColumnToSort = 0; private CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer(); private SortOrder OrderOfSort = SortOrder.None; #endregion #region 属性 // Properties public SortOrder Order { get { return this.OrderOfSort; } set { this.OrderOfSort = value; } } public int SortColumn { get { return this.ColumnToSort; } set { this.ColumnToSort = value; } } #endregion #region 方法 // Methods /// <summary> /// 依据double类型列的值比较 /// </summary> /// <param name="x">ListViewItem</param> /// <param name="y">ListViewItem</param> /// <returns></returns> public int Compare(object x, object y) { ListViewItem item = (ListViewItem)x; ListViewItem item2 = (ListViewItem)y; int num = this.ObjectCompare.Compare(ConvertHelper.ObjectToDouble(item.SubItems[this.ColumnToSort].Text), ConvertHelper.ObjectToDouble(item2.SubItems[this.ColumnToSort].Text)); if (this.OrderOfSort == SortOrder.Ascending) { return num; } if (this.OrderOfSort == SortOrder.Descending) { return -num; } return 0; } #endregion } #endregion } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论