C#中DataGridView操作技巧
本文实例讲述了C#中DataGridView操作技巧。分享给大家供大家参考。具体分析如下:
#region操作DataGridView
///<summary> ///初始化DataGridView属性 ///</summary> ///<paramname="dg">要处理的DataGridView</param> ///<paramname="allowUserToAddRows">允许用户添加行</param> ///<paramname="allowUserToDeleteRows">允许用户删除行</param> ///<paramname="rowHeadersVisible">是否显示包含行标题的列</param> ///<paramname="columnHeadersHeightSizeMode">列标头高度大小模式</param> ///<paramname="multiSelect">是否多选</param> ///<paramname="readOnly">是否只读</param> ///<paramname="rowHeadersWidth">列头宽度</param> ///<paramname="selectionMode">列选择模式</param> publicvirtualvoidInitSetDataGridViewAttribute(DataGridViewdg, boolallowUserToAddRows, boolallowUserToDeleteRows, boolallowUserToResizeRows, boolrowHeadersVisible, DataGridViewColumnHeadersHeightSizeModecolumnHeadersHeightSizeMode, boolmultiSelect, boolreadOnly, introwHeadersWidth, DataGridViewSelectionModeselectionMode) { dg.AllowUserToAddRows=allowUserToAddRows; dg.AllowUserToDeleteRows=allowUserToDeleteRows; dg.AllowUserToResizeRows=allowUserToResizeRows; dg.RowHeadersVisible=rowHeadersVisible; dg.ColumnHeadersHeightSizeMode=columnHeadersHeightSizeMode; dg.MultiSelect=multiSelect; dg.ReadOnly=readOnly; dg.RowHeadersWidth=rowHeadersWidth; dg.SelectionMode=selectionMode;
dg.RowStateChanged+=dg_RowStateChanged; }
///<summary> ///初始化DataGridView属性 ///</summary> ///<paramname="dg">要处理的DataGridView</param> publicvirtualvoidInitSetDataGridViewAttribute(DataGridViewdg) { InitSetDataGridViewAttribute(dg, false, false, false, true,DataGridViewColumnHeadersHeightSizeMode.AutoSize, false, true, 50, DataGridViewSelectionMode.FullRowSelect); }
///<summary> /// ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> publicvirtualvoiddg_RowStateChanged(objectsender,DataGridViewRowStateChangedEventArgse) { e.Row.HeaderCell.Value=(e.Row.Index+1).ToString(); }
///<summary> ///DataGridView添加行 ///</summary> ///<paramname="dg">要处理的DataGridView</param> ///<paramname="dr">添加的列</param> publicvoidDataGridViewAddRows(DataGridViewdg,DataGridViewRowdr) { dg.Rows.Add(dr); }
///<summary> ///DataGridView行中添加列 ///</summary> ///<paramname="objs">要添加的对象</param> ///<returns>列</returns> publicDataGridViewRowDataGridViewRowsAddCells(object[]objs) { DataGridViewRowrow=newDataGridViewRow();
foreach(objectobjinobjs) { DataGridViewTextBoxCelltBoxCell=newDataGridViewTextBoxCell(); tBoxCell.Value=obj;
row.Cells.Add(tBoxCell); }
returnrow; }
///<summary> ///DataGridView添加行 ///</summary> ///<paramname="dg">要处理的DataGridView</param> ///<paramname="listObjecArray">添加的对象List</param> publicvoidDataGridViewAddRows(DataGridViewdg,List<object[]>listObjecArray) { foreach(object[]objsinlistObjecArray) { DataGridViewRowdr=DataGridViewRowsAddCells(objs);
DataGridViewAddRows(dg,dr); } }
///<summary> ///DataGridView添加行 ///</summary> ///<paramname="dg">要处理的DataGridView</param> ///<paramname="objs">添加的对象</param> publicvoidDataGridViewAddRows(DataGridViewdg,object[]objs) { DataGridViewRowdr=DataGridViewRowsAddCells(objs);
DataGridViewAddRows(dg,dr); }
///<summary> ///DataGridView列排序 ///</summary> ///<paramname="dg">要排序的DataGridView</param> ///<paramname="dataGridViewColumnIndex">列索引</param> ///<paramname="flag">0:升序排列1:降序排列</param> publicvoidDataGridViewSort(DataGridViewdg,intdataGridViewColumnIndex,intflag) { switch(flag) { case0: dg.Sort(dg.Columns[dataGridViewColumnIndex],ListSortDirection.Ascending); break; case1: dg.Sort(dg.Columns[dataGridViewColumnIndex],ListSortDirection.Descending); break; default: break; }
}
#endregion