c# DataGridView操作

  1         #region 操作DataGridView
  2 
  3         /// <summary>
  4         /// 初始化DataGridView属性
  5         /// </summary>
  6         /// <param name="dg">要处理的DataGridView</param>
  7         /// <param name="allowUserToAddRows">允许用户添加行</param>
  8         /// <param name="allowUserToDeleteRows">允许用户删除行</param>
  9         /// <param name="rowHeadersVisible">是否显示包含行标题的列</param>
 10         /// <param name="columnHeadersHeightSizeMode">列标头高度大小模式</param>
 11         /// <param name="multiSelect">是否多选</param>
 12         /// <param name="readOnly">是否只读</param>
 13         /// <param name="rowHeadersWidth">列头宽度</param>
 14         /// <param name="selectionMode">列选择模式</param>
 15         public virtual void InitSetDataGridViewAttribute(DataGridView dg,
 16             bool allowUserToAddRows,
 17             bool allowUserToDeleteRows,
 18             bool allowUserToResizeRows,
 19             bool rowHeadersVisible,
 20             DataGridViewColumnHeadersHeightSizeMode columnHeadersHeightSizeMode,
 21             bool multiSelect,
 22             bool readOnly,
 23             int rowHeadersWidth,
 24             DataGridViewSelectionMode selectionMode)
 25         {
 26             dg.AllowUserToAddRows = allowUserToAddRows;
 27             dg.AllowUserToDeleteRows = allowUserToDeleteRows;
 28             dg.AllowUserToResizeRows = allowUserToResizeRows;
 29             dg.RowHeadersVisible = rowHeadersVisible;
 30             dg.ColumnHeadersHeightSizeMode = columnHeadersHeightSizeMode;
 31             dg.MultiSelect = multiSelect;
 32             dg.ReadOnly = readOnly;
 33             dg.RowHeadersWidth = rowHeadersWidth;
 34             dg.SelectionMode = selectionMode;
 35 
 36             dg.RowStateChanged += dg_RowStateChanged;
 37         }
 38 
 39         /// <summary>
 40         /// 初始化DataGridView属性
 41         /// </summary>
 42         /// <param name="dg">要处理的DataGridView</param>
 43         public virtual void InitSetDataGridViewAttribute(DataGridView dg)
 44         {
 45             InitSetDataGridViewAttribute(dg,
 46                 false,
 47                 false,
 48                 false,
 49                 true, DataGridViewColumnHeadersHeightSizeMode.AutoSize,
 50                 false,
 51                 true,
 52                 50,
 53                 DataGridViewSelectionMode.FullRowSelect);
 54         }
 55 
 56         /// <summary>
 57         /// 
 58         /// </summary>
 59         /// <param name="sender"></param>
 60         /// <param name="e"></param>
 61         public virtual void dg_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
 62         {
 63             e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();
 64         }
 65 
 66         /// <summary>
 67         /// DataGridView添加行
 68         /// </summary>
 69         /// <param name="dg">要处理的DataGridView</param>
 70         /// <param name="dr">添加的列</param>
 71         public void DataGridViewAddRows(DataGridView dg, DataGridViewRow dr)
 72         {
 73             dg.Rows.Add(dr);
 74         }
 75 
 76         /// <summary>
 77         /// DataGridView行中添加列
 78         /// </summary>
 79         /// <param name="objs">要添加的对象</param>
 80         /// <returns></returns>
 81         public DataGridViewRow DataGridViewRowsAddCells(object[] objs)
 82         {
 83             DataGridViewRow row = new DataGridViewRow();
 84 
 85             foreach (object obj in objs)
 86             {
 87                 DataGridViewTextBoxCell tBoxCell = new DataGridViewTextBoxCell();
 88                 tBoxCell.Value = obj;
 89 
 90                 row.Cells.Add(tBoxCell);
 91             }
 92 
 93             return row;
 94         }
 95 
 96         /// <summary>
 97         /// DataGridView添加行
 98         /// </summary>
 99         /// <param name="dg">要处理的DataGridView</param>
100         /// <param name="listObjecArray">添加的对象List</param>
101         public void DataGridViewAddRows(DataGridView dg, List<object[]> listObjecArray)
102         {
103             foreach (object[] objs in listObjecArray)
104             {
105                 DataGridViewRow dr = DataGridViewRowsAddCells(objs);
106 
107                 DataGridViewAddRows(dg, dr);
108             }
109         }
110 
111         /// <summary>
112         /// DataGridView添加行
113         /// </summary>
114         /// <param name="dg">要处理的DataGridView</param>
115         /// <param name="objs">添加的对象</param>
116         public void DataGridViewAddRows(DataGridView dg, object[] objs)
117         {
118             DataGridViewRow dr = DataGridViewRowsAddCells(objs);
119 
120             DataGridViewAddRows(dg, dr);
121         }
122 
123         /// <summary>
124         /// DataGridView列排序
125         /// </summary>
126         /// <param name="dg">要排序的DataGridView</param>
127         /// <param name="dataGridViewColumnIndex">列索引</param>
128         /// <param name="flag">0:升序排列 1:降序排列</param>
129         public void DataGridViewSort(DataGridView dg, int dataGridViewColumnIndex, int flag)
130         {
131             switch (flag)
132             {
133                 case 0:
134                     dg.Sort(dg.Columns[dataGridViewColumnIndex], ListSortDirection.Ascending);
135                     break;
136                 case 1:
137                     dg.Sort(dg.Columns[dataGridViewColumnIndex], ListSortDirection.Descending);
138                     break;
139                 default:
140                     break;
141             }
142 
143         }
144 
145         #endregion

 

转载于:https://www.cnblogs.com/yuzhou133/p/4112535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值