C#中DataGridView操作技能。本站提示廣大學習愛好者:(C#中DataGridView操作技能)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中DataGridView操作技能正文
本文實例講述了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#法式設計有所贊助。