Devexpress GridView的新增(带Grouping和Sorting)

来源:互联网 发布:soundstructure软件 编辑:程序博客网 时间:2024/06/03 15:05

最近使用devexpress 在用到Gridview的新增功能时,发现如果Gridview有Grouping和Sorting的时候添加进去的数据显示不出来。查了下官网。

直接上代码 

点击打开链接

        private void addNewRowInGroupMode(DevExpress.XtraGrid.Views.Grid.GridView view)
        {
            //Get the handle of the source data row
            //The row will provide group column values for a new row
            int rowHandle = view.GetDataRowHandleByGroupRowHandle(view.FocusedRowHandle);
            //Store group column values
            object[] groupValues = null;
            object[] sortValues = null;

            int groupColumnCount = view.GroupedColumns.Count;
            int sortCount = view.SortedColumns.Count;

            if (groupColumnCount > 0)
            {
                groupValues = new object[groupColumnCount];
                for (int i = 0; i < groupColumnCount; i++)
                {
                    groupValues[i] = view.GetRowCellValue(rowHandle, view.GroupedColumns[i]);
                }
            }

            if (sortCount > 0)
            {
                sortValues = new object[sortCount];
                for (int i = 0; i < sortCount; i++)
                    sortValues[i] = view.GetRowCellValue(rowHandle, view.SortedColumns[i]);
            }


            //Add a new row
            view.AddNewRow();
            //Get the handle of the new row
            int newRowHandle = view.FocusedRowHandle;
            object newRow = view.GetRow(newRowHandle);
            //Set cell values corresponding to group columns
            if (groupColumnCount > 0)
            {
                for (int i = 0; i < groupColumnCount; i++)
                {
                    view.SetRowCellValue(newRowHandle, view.GroupedColumns[i], groupValues[i]);
                }
            }
            //Set cell values corresponding to sort columns
            if (sortCount > 0)
            {
                for (int i = 0; i < groupColumnCount; i++)
                {
                    view.SetRowCellValue(newRowHandle, view.SortedColumns[i], sortValues[i]);
                }
            }
            //Accept the new row
            //The row moves to a new position according to the current group settings
            view.UpdateCurrentRow();
            //Locate the new row
            for (int n = 0; n < view.DataRowCount; n++)
            {
                if (view.GetRow(n).Equals(newRow))
                {
                    view.FocusedRowHandle = n;
                    break;
                }
            }
        }

原创粉丝点击