Skip to the content

DataGrid control in the repeatable mode

In this example it is shown how to work with a Data Grid control in the repeatable mode. Repeatable mode allows to use other form pages as a detail editing view of the records stored in the data grid control

The main form with data grid opens the child form as a editing dialog for the grid.

DataGrid repeatable mode can be turned on by two ways:

1) By the UI properties of DataGrid control in the designer:

 

2) By the runtime code, which can be seen below or in the attached sample project.

Main form datagrid control code behing shows how to setup DataGrid control into repeatable mode programmatically.

The button „Open dialog“ opens the other form (testOpenWindowDialog1) as a modal dialog. If there are previously saved data the data are loaded to the dialog before it is shown.
If the dialog is closed with the Save button, the transformed (xml) data is saved in the main form.

The repeating grid opens a dialog form (the same as the button) for its each row a user double clicks. Transformed data handling in the grid is automatic and the data propagates to the forms final transformed xml data.

public static partial class FormCodeClass
{
    //...
    static XDocument _xData;
    
    private static NDTextBox txtDialogData
    {
        get { return ThisForm.FindElement("txtDialogData");}
    }

    // Form’s Loaded event handler
    public static void Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var dg = ThisForm.FindElement("NDDataGrid1");
        // get Uid of the dialog form from metadata of this form
        string dialogUid = ThisForm.GetMetadataValue("OpenDialogUid1");
        // initiate repeatable mode – Params: string FormUid, int FormVersion, string FormLabel, int FormPage (0-based index!) 
        //  FormLabel and FormVersion are exclusive, if both are used, the label has precedence!
        dg.InitRepeatableMode(dialogUid, null, null, 0);
        // InitRepeatableMode method is the only thing needed, everything else is automatic…
    }
    // Button BtnOpenWindow Click event handler
    public static void BtnOpenWindow_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // get Uid of the dialog form from metadata of this form
        string dialogUid = ThisForm.GetMetadataValue("OpenDialogUid1");
        // open dialog window, Params: string FormUid, int/string FormVersion/Label, int FormPage (0-based index!) 
        //  FormVersionOrLabel - when this param is omitted ort he label is null the last version of the form is used!
        ChildForm form = ThisForm.CreateDialog(dialogUid, null, 0);
        form.Closed += Form_Closed;
        
        // set data to the dialog if there are any
        if (_xData != null)
            form.LoadExternalData(_xData, null);
        // ...
        
        // open the dialog
        form.Show();
    }
    // ChildForm Close event handler
    public static void Form_Closed(object sender, FormClosedEventArgs e)
    {
        // ...
        
        var form = (ChildForm)sender;
        if (e.DialogResult == true)
        {
            XDocument xData = form.GetTransformedData(null);
            txtDialogData.Text = xData.ToString();
            _xData = xData;
        }
        else if (e.DialogResult == false)
        {
            txtDialogData.Text = "Dialog has been cancelled!";
        }
        // ...
    }

    // ...

    // Button BtnShowData
    public static void BtnShowData_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        XDocument xml = ThisForm.GetTransformedData(null);
        txtDialogData.Text = xml.ToString();
    }
}

The code below is the part of dialog editing form and contains button handlers for Save and Cancel buttons.

The button “Save” validates form’s data and if it is ok, closes the dialog with DialogResult=true. Button “Cancel” closes dialog with DialogResult=false.

public static partial class FormCodeClass
{
    // ...

    // Button btnOK Click event handler
    public static void btnOK_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // if the form is valid, close the form with DialogResult=true !
        if (ThisForm.IsValid())
            ThisForm.Close(true);
    }

    // Button btnCancel Click event handler
    public static void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // close the dialog with DialogResult=false !
        ThisForm.Close(false);
    }
}