时间:2021-05-20
本文实例讲述了C#中DataGridView操作技巧。分享给大家供大家参考。具体分析如下:
复制代码 代码如下:#region 操作DataGridView
/// <summary>
/// 初始化DataGridView属性
/// </summary>
/// <param name="dg">要处理的DataGridView</param>
/// <param name="allowUserToAddRows">允许用户添加行</param>
/// <param name="allowUserToDeleteRows">允许用户删除行</param>
/// <param name="rowHeadersVisible">是否显示包含行标题的列</param>
/// <param name="columnHeadersHeightSizeMode">列标头高度大小模式</param>
/// <param name="multiSelect">是否多选</param>
/// <param name="readOnly">是否只读</param>
/// <param name="rowHeadersWidth">列头宽度</param>
/// <param name="selectionMode">列选择模式</param>
public virtual void InitSetDataGridViewAttribute(DataGridView dg,
bool allowUserToAddRows,
bool allowUserToDeleteRows,
bool allowUserToResizeRows,
bool rowHeadersVisible,
DataGridViewColumnHeadersHeightSizeMode columnHeadersHeightSizeMode,
bool multiSelect,
bool readOnly,
int rowHeadersWidth,
DataGridViewSelectionMode selectionMode)
{
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>
/// <param name="dg">要处理的DataGridView</param>
public virtual void InitSetDataGridViewAttribute(DataGridView dg)
{
InitSetDataGridViewAttribute(dg,
false,
false,
false,
true, DataGridViewColumnHeadersHeightSizeMode.AutoSize,
false,
true,
50,
DataGridViewSelectionMode.FullRowSelect);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void dg_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();
}
/// <summary>
/// DataGridView添加行
/// </summary>
/// <param name="dg">要处理的DataGridView</param>
/// <param name="dr">添加的列</param>
public void DataGridViewAddRows(DataGridView dg, DataGridViewRow dr)
{
dg.Rows.Add(dr);
}
/// <summary>
/// DataGridView行中添加列
/// </summary>
/// <param name="objs">要添加的对象</param>
/// <returns>列</returns>
public DataGridViewRow DataGridViewRowsAddCells(object[] objs)
{
DataGridViewRow row = new DataGridViewRow();
foreach (object obj in objs)
{
DataGridViewTextBoxCell tBoxCell = new DataGridViewTextBoxCell();
tBoxCell.Value = obj;
row.Cells.Add(tBoxCell);
}
return row;
}
/// <summary>
/// DataGridView添加行
/// </summary>
/// <param name="dg">要处理的DataGridView</param>
/// <param name="listObjecArray">添加的对象List</param>
public void DataGridViewAddRows(DataGridView dg, List<object[]> listObjecArray)
{
foreach (object[] objs in listObjecArray)
{
DataGridViewRow dr = DataGridViewRowsAddCells(objs);
DataGridViewAddRows(dg, dr);
}
}
/// <summary>
/// DataGridView添加行
/// </summary>
/// <param name="dg">要处理的DataGridView</param>
/// <param name="objs">添加的对象</param>
public void DataGridViewAddRows(DataGridView dg, object[] objs)
{
DataGridViewRow dr = DataGridViewRowsAddCells(objs);
DataGridViewAddRows(dg, dr);
}
/// <summary>
/// DataGridView列排序
/// </summary>
/// <param name="dg">要排序的DataGridView</param>
/// <param name="dataGridViewColumnIndex">列索引</param>
/// <param name="flag">0:升序排列 1:降序排列</param>
public void DataGridViewSort(DataGridView dg, int dataGridViewColumnIndex, int flag)
{
switch (flag)
{
case 0:
dg.Sort(dg.Columns[dataGridViewColumnIndex], ListSortDirection.Ascending);
break;
case 1:
dg.Sort(dg.Columns[dataGridViewColumnIndex], ListSortDirection.Descending);
break;
default:
break;
}
}
#endregion
希望本文所述对大家的C#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文汇总了C#中DatagridView的常用操作,有助于读者加深对C#DatagridView用法的理解,具体如下:1、(最基本的技巧)、获取某列中的某行(某
本文实例讲述了C#中DataGridView常用操作。分享给大家供大家参考。具体如下:publicvoidBinder1(){DataTabletableTyp
本文实例讲述了C#中DataGridView动态添加行及添加列的方法。分享给大家供大家参考。具体如下:Datagridview添加列:DataGridViewT
本文实例讲述了C#中datagridview的EditingControlShowing事件用法。分享给大家供大家参考。具体实现方法如下:usingSystem
本文实例讲述了C#实现将数据导出到word或者Excel中的方法。分享给大家供大家参考。具体如下:voidOutToWord(){if(dataGridView