• Using a layout file to preserve user changes [Janus GridEX WinForms Control v3.5 for .NET]

    This tutorial is intended to show you how to use a layout file to preserve GridEX control settings. Using a layout file you could be able to preserve the changes to the layout made by the user.



    The Layout file used in this tutorial was saved with the GridEX designer using the multiple layouts defined in tutorial 13.



    Follow these steps to use a layout file at run time:



    1 - Define the layout at design time and then save it clicking in the "Save Layout File" button that is found in the "Layout Manager" tab of the GridEX control designer.



    2 - (Optional) Clear all the settings of the GridEX control at run time clicking in the menu "Reset Defaults" that appears when you right click a GridEX control.



    3 - In the Load event of the form, load the layout from a file calling a procedure similar to the following:



    In VB:



    Private Sub LoadLayout()

    Dim LayoutDir As String = GetLayoutDirectory() + "\GridEXLayout.gxl"

    Dim LayoutStream As FileStream

    LayoutStream = New FileStream(LayoutDir, FileMode.Open)

    GridEX1.LoadLayoutFile(LayoutStream)

    LayoutStream.Close()

    End Sub



    In C#:



    private void LoadLayout()

    {

    string layoutDir = GetLayoutDirectory() + @"\GridEXLayout.gxl";

    if (FileExists(layoutDir))

    {

    FileStream layoutStream;

    layoutStream = new FileStream(layoutDir, FileMode.Open);

    GridEX1.LoadLayoutFile(layoutStream);

    layoutStream.Close();

    }

    }



    4 - In the CurrentLayoutChanged event, bound the GridEX control to its data source and fill the data source as follows:



    In VB:



    Private Sub GridEX1_CurrentLayoutChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridEX1.CurrentLayoutChanged

    'clear the DataTable used by the previous layout

    JsNorthWindDataSet1.Clear()

    'when layouts are persisted into a file,

    'the DataSource and DataMember properties are

    'not persisted so you must reset them at run time

    'instead of resetting the DataSource and DataMember

    'properties when the layout is made, this could be done

    'in all the layouts at once in the LayoutLoad event

    If Not GridEX1.CurrentLayout Is Nothing Then

    Select Case GridEX1.CurrentLayout.Key

    Case "Customers"

    CustomersTableAdapter1.Fill(JsNorthWindDataSet1.Customers)

    GridEX1.SetDataBinding(JsNorthWindDataSet1, "Customers")

    Case "Products"

    ProductsTableAdapter1.Fill(JsNorthWindDataSet1.Products)

    GridEX1.SetDataBinding(JsNorthWindDataSet1, "Products")

    Case "Suppliers"

    SuppliersTableAdapter1.Fill(JsNorthWindDataSet1.Suppliers)

    GridEX1.SetDataBinding(JsNorthWindDataSet1, "Suppliers")

    End Select

    End If



    End Sub



    In C#:



    private void GridEX1_CurrentLayoutChanged(object sender, EventArgs e)

    {

    //clear the DataTable used by the previous layout

    jsNorthWindDataSet1.Clear();



    //When layouts are persisted into a file,

    //the DataSource and DataMember properties are

    //not persisted so you must reset them at run time

    //instead of resetting the DataSource and DataMember

    //properties when the layout is made, this could be done

    //in all the layouts at once in the LayoutLoad event

    if (GridEX1.CurrentLayout != null)

    {

    switch (GridEX1.CurrentLayout.Key)

    {

    case "Customers":

    customersTableAdapter1.Fill(jsNorthWindDataSet1.Customers);

    GridEX1.SetDataBinding(jsNorthWindDataSet1, "Customers");

    break;

    case "Products":

    productsTableAdapter1.Fill(jsNorthWindDataSet1.Products);

    GridEX1.SetDataBinding(jsNorthWindDataSet1, "Products");

    break;

    case "Suppliers":

    suppliersTableAdapter1.Fill(jsNorthWindDataSet1.Suppliers);

    GridEX1.SetDataBinding(jsNorthWindDataSet1, "Suppliers");

    break;

    }

    }

    }



    5- (Optional) To preserve user changes to the layout, update each layout before it is changed in the CurrentLayoutChanging event.



    In VB:



    Private Sub GridEX1_CurrentLayoutChanging(ByVal sender As Object, _

    ByVal e As System.ComponentModel.CancelEventArgs) Handles _

    GridEX1.CurrentLayoutChanging

    'to persist user changes in the current layout,

    'call the Update method explicitly before changing the layout

    If Not GridEX1.CurrentLayout Is Nothing Then

    GridEX1.CurrentLayout.Update()

    End If

    End Sub



    In C#:



    private void gridEX1_CurrentLayoutChanging(object sender, System.ComponentModel.CancelEventArgs e)

    {

    //to persist user changes in the current layout,

    //call the Update method explicitly before changing the layout

    if(gridEX1.CurrentLayout!=null)

    {

    gridEX1.CurrentLayout.Update();

    }

    }



    6 - In the Closing event of the form, save the layout file again to be able to preserve the changes the user did (like grouping, sorting, columns size and position etc).



    In VB:



    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)

    Dim Result As DialogResult

    Dim LayoutDir As String

    Dim LayoutStream As FileStream





    Result = MessageBox.Show("Do you want to preserve the changes in the _

    GridEX control layout?", "Preserve changes", _

    MessageBoxButtons.YesNoCancel, _

    MessageBoxIcon.Question)

    If Result = DialogResult.Cancel Then

    e.Cancel = True

    ElseIf Result = DialogResult.Yes Then

    LayoutDir = GetLayoutDirectory() + "\GridEXLayout.gxl"

    LayoutStream = New FileStream(LayoutDir, FileMode.Create)

    GridEX1.SaveLayoutFile(LayoutStream)

    LayoutStream.Close()

    End If

    End Sub



    In C #:



    protected override void OnClosing(CancelEventArgs e)

    {

    base.OnClosing(e);

    DialogResult result;

    string layoutDir;

    FileStream layoutStream;



    result = MessageBox.Show("Do you want to preserve the changes in " +

    "the GridEX control layout?", "Preserve changes",

    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

    if (result == System.Windows.Forms.DialogResult.Cancel)

    {

    e.Cancel = true;

    }

    else if (result == System.Windows.Forms.DialogResult.Yes)

    {

    DirectoryInfo dInfo;

    dInfo = new DirectoryInfo(Application.ExecutablePath).Parent;

    dInfo = new DirectoryInfo(dInfo.FullName + @"\LayoutData");

    if (!dInfo.Exists) dInfo.Create();

    layoutDir = dInfo.FullName + @"\GridEXLayout.gxl";

    layoutStream = new FileStream(layoutDir, FileMode.Create);

    GridEX1.SaveLayoutFile(layoutStream);

    layoutStream.Close();

    }

    }



    7 - Run the project.

    Source Of Information : Janus v3.5 Help Files for VS 2008


0 comments:

Leave a Reply