Skip to the content

Using NDDataGrid in code-behind

This post describes some specific operations that are possible to do with the e-Form System's NDDataGrid; for example disabling/enabling specific cells or bind custom items to a combo box column etc..

 

Disabling grid cells

Here are some examples of how to programmatically disable and enable cells in a DataGrid.

Disable DataGrid's current cell code sample

Following code snippet shows how to enable (or disable) the currently focused cell of a grid. This sample assumes an e-form with these conditions:

  • the form contains control of type Data Grid (class Neurodot.Forms.Controls.NDDataGrid) named Grid1
  • the Grid1 contains 2 or more rows
  • the Grid1 has 1 or more columns
var grid = ThisForm.FindElement<NDDataGrid>("Grid1");
Row row = grid.CurrentRow;
if (row != null)
{
    DataGridColumn col = grid.CurrentColumn;
    if (col != null)
    {
        grid.EnableCell(row, col, false); // false to disable the cell; true to enable it
    }
}

Disable DataGrid's specific cell code sample

The next code snippet shows how to enable (or disable) a cell specified by row index and the name of a column. This sample assumes an e-form with these conditions:

  • the form contains control of type Data Grid (class Neurodot.Forms.Controls.NDDataGrid) named Grid1
  • the Grid1 contains 2 or more rows
  • the Grid1 has at least one column one of which is named TextColumn1" (SortMemberPath property of a column determines its name in our case)

 

/// <summary>This method enables/disables a cell of the grid named Grid1.</summary>
/// <param name="rowIndex">Index of the row containing the cell to enable/disable.</param>
/// <param name="columnName">Name of the column containing the cell to enable/disable (i.e. value of the SortMemberPath property of the column).</param>
/// <param name="enable">A value indicating whether to enable or disable the cell; true to enable.</param>
public static void EnableGridCell(int rowIndex, string columnName, bool enable)
{
    var grid = ThisForm.FindElement<NDDataGrid>("Grid1");
    var rows = grid.ItemsSource as IList<Row>;
    Row row = rows[rowIndex];
    var col = grid.GetColumnBySortMemberPath(columnName);
    grid.EnableCell(row, col, enable);
}

Binding custom items to CombobBox columns

There is a difference in behavior of combo box columns' items in localized and non-localized forms (localized forms have at least one localization file attached via the e-Forms Designer).
In non-localized forms the texts of items of combo columns are taken directly from the designed template of the form and normally never change; but in localized forms the items' texts are taken from the active localization file and are rewritten every time the language of the form is changed (e.g. by an end user). Both types of forms have an easy way to setup custom items programmatically from code-behind of the form; here's how to do it...

In forms without localizations

In non-localized forms a method of the NDDataGrid class SetComboBoxColumnItems can be used to set items of combo box column. The first parameter of the method can be either a reference to the column itself (the type of the parameter is DataGridTemplateColumn) or the name of the column (i.e. the value of the SortMemberPart property of the column); the second parameter of the method is the list of items which is of type IEnumerable<TextValueItem>.

The following example assumes these conditions:

  • the form contains control of type Data Grid (class Neurodot.Forms.Controls.NDDataGrid) named Grid1
  • the Grid1 has at least one column one of which is named "ComboColumn1" (SortMemberPath property of a column determines its name as a rule)
  • to test that the items are properly set the grid must have at least one row
var grid = ThisForm.FindElement("Grid1");
var items = new List();
items.Add(new TextValueItem("ComboItem #1", "Value1"));
items.Add(new TextValueItem("ComboItem #2", "Value2"));
grid.SetComboBoxColumnItems("ComboColumn1", items);

In forms with localizations

In localized forms the NDDataGrid class offers method RegisterComboBoxColumnItems that can be used to register items of combo box column for all possible languages the form can be localized to. The parameters of the method are almost the same as before; the first one can be either a reference to the column itself (the type of the parameter is DataGridTemplateColumn) or the name of the column (i.e. the value of the SortMemberPart property of the column); the second parameter of the method is the list of items which is of type IEnumerable<LocalizedTextValueItem>. The LocalizedTextValueItem class carries extra information about a language (i.e. the culture code) for which the items is to be registered.

Note: If there is no need to dynamically change the items thruout the life of the form it's best to register the combo box column items in the handler method of the form's Loaded event.

The following example assumes these conditions:

  • the form contains control of type Data Grid (class Neurodot.Forms.Controls.NDDataGrid) named Grid1
  • the Grid1 has at least one column one of which is named "ComboColumn1" (SortMemberPath property of a column determines its name as a rule)
  • to test that the items are properly set the grid must have at least one row
var grid = ThisForm.FindElement("Grid1");
var items = new List();
items.Add(new LocalizedTextValueItem("Položka 1", "Value1", "cs-CZ")); // items for Czech localization
items.Add(new LocalizedTextValueItem("Položka 2", "Value2", "cs-CZ"));
items.Add(new LocalizedTextValueItem("Item #1", "Value1", "en-US"));  // items for English localization
items.Add(new LocalizedTextValueItem("Item #2", "Value2", "en-US"));
items.Add(new LocalizedTextValueItem("Item #3", "Value3", "en-US")); // number of items can differ for different languages
grid.RegisterComboBoxColumnItems("ComboColumn1", items);