This example creates a helper class that allows you to save and restore the following View settings:
- Group row expansion state
- Detail expansion state
- Multiple row selection
- Focused row
Files to Review
- Form1.cs (VB: Form1.vb)
- RefreshHelperClass.cs (VB: RefreshHelperClass.vb)
See Also
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Helpers;
namespace RefreshHelperApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private DevExpress.XtraGrid.GridControl gridControl1;
private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
private DevExpress.XtraEditors.SimpleButton simpleButton1;
private DevExpress.XtraEditors.SimpleButton simpleButton2;
private DevExpress.XtraGrid.Views.Grid.GridView gridView2;
private DevExpress.XtraGrid.Columns.GridColumn colCustomerID1;
private DevExpress.XtraGrid.Columns.GridColumn colEmployeeID;
private DevExpress.XtraGrid.Columns.GridColumn colFreight;
private DevExpress.XtraGrid.Columns.GridColumn colOrderDate;
private DevExpress.XtraGrid.Columns.GridColumn colOrderID;
private DevExpress.XtraGrid.Columns.GridColumn colRequiredDate;
private DevExpress.XtraGrid.Columns.GridColumn colShipAddress;
private DevExpress.XtraGrid.Columns.GridColumn colShipCity;
private DevExpress.XtraGrid.Columns.GridColumn colShipCountry;
private DevExpress.XtraGrid.Columns.GridColumn colShipName;
private DevExpress.XtraGrid.Columns.GridColumn colShippedDate;
private DevExpress.XtraGrid.Columns.GridColumn colShipPostalCode;
private DevExpress.XtraGrid.Columns.GridColumn colShipRegion;
private DevExpress.XtraGrid.Columns.GridColumn colShipVia;
private System.Windows.Forms.Panel panel1;
private IContainer components;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
DevExpress.XtraGrid.GridLevelNode gridLevelNode1 = new DevExpress.XtraGrid.GridLevelNode();
this.gridView2 = new DevExpress.XtraGrid.Views.Grid.GridView();
this.colCustomerID1 = new DevExpress.XtraGrid.Columns.GridColumn();
this.colEmployeeID = new DevExpress.XtraGrid.Columns.GridColumn();
this.colFreight = new DevExpress.XtraGrid.Columns.GridColumn();
this.colOrderDate = new DevExpress.XtraGrid.Columns.GridColumn();
this.colOrderID = new DevExpress.XtraGrid.Columns.GridColumn();
this.colRequiredDate = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipAddress = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipCity = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipCountry = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipName = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShippedDate = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipPostalCode = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipRegion = new DevExpress.XtraGrid.Columns.GridColumn();
this.colShipVia = new DevExpress.XtraGrid.Columns.GridColumn();
this.gridControl1 = new DevExpress.XtraGrid.GridControl();
this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
this.simpleButton1 = new DevExpress.XtraEditors.SimpleButton();
this.simpleButton2 = new DevExpress.XtraEditors.SimpleButton();
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.gridView2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// gridView2
//
this.gridView2.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
this.colCustomerID1,
this.colEmployeeID,
this.colFreight,
this.colOrderDate,
this.colOrderID,
this.colRequiredDate,
this.colShipAddress,
this.colShipCity,
this.colShipCountry,
this.colShipName,
this.colShippedDate,
this.colShipPostalCode,
this.colShipRegion,
this.colShipVia});
this.gridView2.GridControl = this.gridControl1;
this.gridView2.Name = "gridView2";
//
// colCustomerID1
//
this.colCustomerID1.Caption = "CustomerID";
this.colCustomerID1.FieldName = "CustomerID";
this.colCustomerID1.Name = "colCustomerID1";
this.colCustomerID1.Visible = true;
this.colCustomerID1.VisibleIndex = 0;
//
// colEmployeeID
//
this.colEmployeeID.Caption = "EmployeeID";
this.colEmployeeID.FieldName = "EmployeeID";
this.colEmployeeID.Name = "colEmployeeID";
this.colEmployeeID.Visible = true;
this.colEmployeeID.VisibleIndex = 1;
//
// colFreight
//
this.colFreight.Caption = "Freight";
this.colFreight.FieldName = "Freight";
this.colFreight.Name = "colFreight";
this.colFreight.Visible = true;
this.colFreight.VisibleIndex = 2;
//
// colOrderDate
//
this.colOrderDate.Caption = "OrderDate";
this.colOrderDate.FieldName = "OrderDate";
this.colOrderDate.Name = "colOrderDate";
this.colOrderDate.Visible = true;
this.colOrderDate.VisibleIndex = 3;
//
// colOrderID
//
this.colOrderID.Caption = "OrderID";
this.colOrderID.FieldName = "OrderID";
this.colOrderID.Name = "colOrderID";
this.colOrderID.Visible = true;
this.colOrderID.VisibleIndex = 4;
//
// colRequiredDate
//
this.colRequiredDate.Caption = "RequiredDate";
this.colRequiredDate.FieldName = "RequiredDate";
this.colRequiredDate.Name = "colRequiredDate";
this.colRequiredDate.Visible = true;
this.colRequiredDate.VisibleIndex = 5;
//
// colShipAddress
//
this.colShipAddress.Caption = "ShipAddress";
this.colShipAddress.FieldName = "ShipAddress";
this.colShipAddress.Name = "colShipAddress";
this.colShipAddress.Visible = true;
this.colShipAddress.VisibleIndex = 6;
//
// colShipCity
//
this.colShipCity.Caption = "ShipCity";
this.colShipCity.FieldName = "ShipCity";
this.colShipCity.Name = "colShipCity";
this.colShipCity.Visible = true;
this.colShipCity.VisibleIndex = 7;
//
// colShipCountry
//
this.colShipCountry.Caption = "ShipCountry";
this.colShipCountry.FieldName = "ShipCountry";
this.colShipCountry.Name = "colShipCountry";
this.colShipCountry.Visible = true;
this.colShipCountry.VisibleIndex = 8;
//
// colShipName
//
this.colShipName.Caption = "ShipName";
this.colShipName.FieldName = "ShipName";
this.colShipName.Name = "colShipName";
this.colShipName.Visible = true;
this.colShipName.VisibleIndex = 9;
//
// colShippedDate
//
this.colShippedDate.Caption = "ShippedDate";
this.colShippedDate.FieldName = "ShippedDate";
this.colShippedDate.Name = "colShippedDate";
this.colShippedDate.Visible = true;
this.colShippedDate.VisibleIndex = 10;
//
// colShipPostalCode
//
this.colShipPostalCode.Caption = "ShipPostalCode";
this.colShipPostalCode.FieldName = "ShipPostalCode";
this.colShipPostalCode.Name = "colShipPostalCode";
this.colShipPostalCode.Visible = true;
this.colShipPostalCode.VisibleIndex = 11;
//
// colShipRegion
//
this.colShipRegion.Caption = "ShipRegion";
this.colShipRegion.FieldName = "ShipRegion";
this.colShipRegion.Name = "colShipRegion";
this.colShipRegion.Visible = true;
this.colShipRegion.VisibleIndex = 12;
//
// colShipVia
//
this.colShipVia.Caption = "ShipVia";
this.colShipVia.FieldName = "ShipVia";
this.colShipVia.Name = "colShipVia";
this.colShipVia.Visible = true;
this.colShipVia.VisibleIndex = 13;
//
// gridControl1
//
this.gridControl1.Dock = System.Windows.Forms.DockStyle.Fill;
gridLevelNode1.LevelTemplate = this.gridView2;
gridLevelNode1.RelationName = "CustomersOrders";
this.gridControl1.LevelTree.Nodes.AddRange(new DevExpress.XtraGrid.GridLevelNode[] {
gridLevelNode1});
this.gridControl1.Location = new System.Drawing.Point(0, 40);
this.gridControl1.MainView = this.gridView1;
this.gridControl1.Name = "gridControl1";
this.gridControl1.Size = new System.Drawing.Size(875, 510);
this.gridControl1.TabIndex = 0;
this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
this.gridView1,
this.gridView2});
//
// gridView1
//
this.gridView1.GridControl = this.gridControl1;
this.gridView1.Name = "gridView1";
this.gridView1.OptionsSelection.MultiSelect = true;
//
// simpleButton1
//
this.simpleButton1.Location = new System.Drawing.Point(8, 8);
this.simpleButton1.Name = "simpleButton1";
this.simpleButton1.Size = new System.Drawing.Size(75, 23);
this.simpleButton1.TabIndex = 1;
this.simpleButton1.Text = "Save";
this.simpleButton1.Click += new System.EventHandler(this.simpleButton1_Click);
//
// simpleButton2
//
this.simpleButton2.Location = new System.Drawing.Point(104, 8);
this.simpleButton2.Name = "simpleButton2";
this.simpleButton2.Size = new System.Drawing.Size(72, 24);
this.simpleButton2.TabIndex = 2;
this.simpleButton2.Text = "Load";
this.simpleButton2.Click += new System.EventHandler(this.simpleButton2_Click);
//
// panel1
//
this.panel1.Controls.Add(this.simpleButton1);
this.panel1.Controls.Add(this.simpleButton2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(875, 40);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(875, 550);
this.Controls.Add(this.gridControl1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.gridView2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
RefreshHelper helper;
private void Form1_Load(object sender, System.EventArgs e)
{
helper = new RefreshHelper(gridView1, "ID");
DataSet dataSet11 = new DataSet();
dataSet11.Tables.Add(GetCustomerDataTable());
dataSet11.Tables.Add(GetPersonDataTable());
DataColumn keyColumn = dataSet11.Tables["Customers"].Columns["ID"];
DataColumn foreignKeyColumn = dataSet11.Tables["Persons"].Columns["ID"];
dataSet11.Relations.Add("CustomersPersons", keyColumn, foreignKeyColumn);
gridControl1.DataSource = dataSet11.Tables["Customers"];
gridControl1.ForceInitialize();
}
private void simpleButton1_Click(object sender, System.EventArgs e)
{
helper.SaveViewInfo();
}
private void simpleButton2_Click(object sender, System.EventArgs e)
{
helper.LoadViewInfo();
}
DataTable GetCustomerDataTable()
{
DataTable table = new DataTable();
table.TableName = "Customers";
table.Columns.Add(new DataColumn("Items", typeof(string)));
table.Columns.Add(new DataColumn("Money", typeof(double)));
table.Columns.Add(new DataColumn("ID", typeof(int)));
for (int i = 0; i < 10; i++)
table.Rows.Add("Product " + i, 3000 + i * 298.55M, i);
return table;
}
DataTable GetPersonDataTable()
{
DataTable table = new DataTable();
table.TableName = "Persons";
table.Columns.Add(new DataColumn("FirstName", typeof(string)));
table.Columns.Add(new DataColumn("SecondName", typeof(string)));
table.Columns.Add(new DataColumn("Age", typeof(int)));
table.Columns.Add(new DataColumn("ID", typeof(int)));
for (int i = 0; i < 50; i++)
{
string name = "Adam";
string secondName = "Smith";
if (i % 2 == 0)
{
name = "Ben";
secondName = "Black";
}
table.Rows.Add(name, secondName, 20 + i / 2, i % 10);
}
return table;
}
}
}
C#using System;
using System.Collections;
using DevExpress.XtraGrid;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
namespace DevExpress.XtraGrid.Helpers {
public class RefreshHelper {
[Serializable]
public struct RowInfo{
public object Id;
public int level;
};
private GridView view;
private string keyFieldName;
private ArrayList saveExpList;
private ArrayList saveSelList;
private ArrayList saveMasterRowsList;
private int visibleRowIndex = -1;
public RefreshHelper(GridView view, string keyFieldName) {
this.view = view;
this.keyFieldName = keyFieldName;
}
public ArrayList SaveExpList{
get {
if(saveExpList == null)
saveExpList = new ArrayList();
return saveExpList;
}
}
public ArrayList SaveSelList{
get {
if(saveSelList == null)
saveSelList = new ArrayList();
return saveSelList;
}
}
public ArrayList SaveMasterRowsList{
get{
if(saveMasterRowsList == null)
saveMasterRowsList = new ArrayList();
return saveMasterRowsList;
}
}
protected int FindParentRowHandle(RowInfo rowInfo, int rowHandle) {
int result = view.GetParentRowHandle(rowHandle);
while(view.GetRowLevel(result) != rowInfo.level && view.IsValidRowHandle(result))
result = view.GetParentRowHandle(result);
return result;
}
protected void ExpandRowByRowInfo(RowInfo rowInfo){
int dataRowHandle = view.LocateByValue(0, view.Columns[keyFieldName], rowInfo.Id);
if(dataRowHandle != GridControl.InvalidRowHandle){
int parentRowHandle = FindParentRowHandle(rowInfo, dataRowHandle);
view.SetRowExpanded(parentRowHandle, true, false);
}
}
protected int GetRowHandleToSelect(RowInfo rowInfo) {
int dataRowHandle = view.LocateByValue(0, view.Columns[keyFieldName], rowInfo.Id);
if(dataRowHandle != GridControl.InvalidRowHandle)
if (view.GetRowLevel(dataRowHandle) != rowInfo.level)
return FindParentRowHandle(rowInfo, dataRowHandle);
return dataRowHandle;
}
protected void SelectRowByRowInfo(RowInfo rowInfo, bool isFocused){
if(isFocused)
view.FocusedRowHandle = GetRowHandleToSelect(rowInfo);
else
view.SelectRow(GetRowHandleToSelect(rowInfo));
}
public void SaveSelectionViewInfo(ArrayList list){
list.Clear();
GridColumn column = view.Columns[keyFieldName];
RowInfo rowInfo;
int[] selectionArray = view.GetSelectedRows();
if (selectionArray != null) // otherwise we have a single focused but not selected row
for(int i = 0; i < selectionArray.Length; i++){
int dataRowHandle = selectionArray[i];
rowInfo.level = view.GetRowLevel(dataRowHandle);
if(dataRowHandle < 0) // group row
dataRowHandle = view.GetDataRowHandleByGroupRowHandle(dataRowHandle);
rowInfo.Id = view.GetRowCellValue(dataRowHandle, column);
list.Add(rowInfo);
}
rowInfo.Id = view.GetRowCellValue(view.FocusedRowHandle, column);
rowInfo.level = view.GetRowLevel(view.FocusedRowHandle);
list.Add(rowInfo);
}
public void SaveExpansionViewInfo(ArrayList list){
if(view.GroupedColumns.Count == 0) return;
list.Clear();
GridColumn column = view.Columns[keyFieldName];
for(int i = -1; i > int.MinValue; i--) {
if(!view.IsValidRowHandle(i)) break;
if(view.GetRowExpanded(i)){
RowInfo rowInfo;
int dataRowHandle = view.GetDataRowHandleByGroupRowHandle(i);
rowInfo.Id = view.GetRowCellValue(dataRowHandle, column);
rowInfo.level = view.GetRowLevel(i);
list.Add(rowInfo);
}
}
}
public void SaveExpandedMasterRows(ArrayList list){
if(view.GridControl.Views.Count == 1) return;
list.Clear();
GridColumn column = view.Columns[keyFieldName];
for(int i = 0; i < view.DataRowCount; i++)
if(view.GetMasterRowExpanded(i))
list.Add(view.GetRowCellValue(i, column));
}
public void SaveVisibleIndex(){
visibleRowIndex = view.GetVisibleIndex(view.FocusedRowHandle) - view.TopRowIndex;
}
public void LoadVisibleIndex(){
view.MakeRowVisible(view.FocusedRowHandle, true);
view.TopRowIndex = view.GetVisibleIndex(view.FocusedRowHandle) - visibleRowIndex;
}
public void LoadSelectionViewInfo(ArrayList list) {
view.BeginSelection();
try {
view.ClearSelection();
for(int i = 0; i < list.Count; i++)
SelectRowByRowInfo((RowInfo)list[i], i == list.Count - 1);
}
finally {
view.EndSelection();
}
}
public void LoadExpansionViewInfo(ArrayList list) {
if(view.GroupedColumns.Count == 0) return;
view.BeginUpdate();
try {
view.CollapseAllGroups();
foreach(RowInfo info in list)
ExpandRowByRowInfo(info);
}
finally {
view.EndUpdate();
}
}
public void LoadExpandedMasterRows(ArrayList list) {
view.BeginUpdate();
try {
view.CollapseAllDetails();
GridColumn column = view.Columns[keyFieldName];
for(int i = 0; i < list.Count; i++){
int rowHandle = view.LocateByValue(0, column, list[i]);
view.SetMasterRowExpanded(rowHandle, true);
}
}
finally {
view.EndUpdate();
}
}
public void SaveViewInfo(){
SaveExpandedMasterRows(SaveMasterRowsList);
SaveExpansionViewInfo(SaveExpList);
SaveSelectionViewInfo(SaveSelList);
SaveVisibleIndex();
}
public void LoadViewInfo(){
LoadExpandedMasterRows(SaveMasterRowsList);
LoadExpansionViewInfo(SaveExpList);
LoadSelectionViewInfo(SaveSelList);
LoadVisibleIndex();
}
}
}