欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中DataGridView操作技巧

程序员文章站 2023-12-15 19:24:04
本文实例讲述了c#中datagridview操作技巧。分享给大家供大家参考。具体分析如下: 复制代码 代码如下:#region 操作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#程序设计有所帮助。

上一篇:

下一篇: