DevExpress GridControl 控件中GridView 加组,加行

DevExpress GridControl 控件是经常会用到  加组 加行功能

扩展方法

public static class Tools
{
      /// <summary>
        /// 加组
        /// </summary>
        /// <param name="GroupFieldName">分组的数据源名称</param>
        public static DevExpress.XtraGrid.GridControl AddGroup(this DevExpress.XtraGrid.GridControl _GridControl, string GroupFieldName) 
        {
            DataTable _Dt = (DataTable)_GridControl.DataSource;
            if (_Dt.Rows.Count <= 0)
            {
                ExtensionMethods.AddItem(_GridControl,GroupFieldName);
                return _GridControl;
            }
            string GroupColumn = _Dt.Rows[_Dt.Rows.Count - 1][GroupFieldName].ToString();
            if (GroupColumn == "")
            {
                _Dt.Rows[_Dt.Rows.Count - 1][GroupFieldName] = "0";
                GroupColumn = "0";
            }
            DataRow _Dr = _Dt.NewRow();
            _Dr[GroupFieldName] = GroupColumn.GetInt() + 1;
            _Dt.Rows.Add(_Dr);
            _GridControl.DataSource = _Dt;
            _GridControl.RefreshDataSource();
            if (_Dt.Rows.Count - 1 >= 0)
            {
                DevExpress.XtraGrid.Views.Grid.GridView _GridView = (DevExpress.XtraGrid.Views.Grid.GridView)_GridControl.MainView;
                _GridView.OptionsNavigation.AutoFocusNewRow = true;
                _GridView.SelectRow(_Dt.Rows.Count - 1);
            }
            return _GridControl;
        }
        /// <summary>
        /// 为分组添加一行
        /// 选中那行,添加到之后
        /// </summary>
        public static DevExpress.XtraGrid.GridControl AddItem(this DevExpress.XtraGrid.GridControl _GridControl, string GroupFieldName)
        {          
            //判断选中的是哪个Tab,


            
            DevExpress.XtraGrid.Views.Grid.GridView _GridView = (DevExpress.XtraGrid.Views.Grid.GridView)_GridControl.MainView;


            int Selct = ((GridView)_GridControl.MainView).FocusedRowHandle;
            int Index = Selct;
            if (Selct > -2147483647 && Selct < 0)
            {
                bool IsGroup = _GridView.IsGroupRow(Selct);
              
                if (IsGroup)
                {
                    Index = _GridView.GetChildRowHandle(Selct, 0);
                }
            }
            string GroupColumn = _GridView.GetRowCellValue(Index, GroupFieldName).GetString();
            _GridView.SetRowCellValue(Index, GroupFieldName, GroupColumn == "" ? (GroupColumn = "0") : GroupColumn);
            DataTable _Dt = (DataTable)_GridControl.DataSource;
            DataRow _Dr = _Dt.NewRow();
            _Dr[GroupFieldName] = GroupColumn == "" ? "0" : GroupColumn;
            _Dt.Rows.Add(_Dr);
            _GridControl.DataSource = _Dt;
            _GridControl.RefreshDataSource();
            _GridView.RefreshData();
            if (Index >= 0)
            {
                _GridView.OptionsNavigation.AutoFocusNewRow = true;
                _GridView.SelectRow(Index);
            }
            return _GridControl;
       
        } 
}
调用 加组
        #region 加组
        private void AddGroupButton_Click(object sender, EventArgs e)
        {
            try
            {
                _GridControl.AddGroup("GROUPING");
            }
            catch (Exception) { }
            
           
        }
        #endregion
调用 加行

 #region 加行
        private void AddItemButton_Click(object sender, EventArgs e)
        {  
          try
            {
                _GridControl.AddItem("GROUPING"); 
            }
            catch (Exception) { }
        }
        #endregion


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值