C#中DataGridView操作技巧_C#教程

分享到:

本文实例讲述了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#程序设计有所帮助。

昵    称:
验证码:

相关文档: