Version

Setting Column Settings for a Behavior

Before You Begin

You can set certain behaviors column-by-column. This allows you to restrict a behavior to only a select number of columns in WebDataGrid™. This is quite useful when you have a primary key column or a column that your end-users should not edit.

For each behavior that you can set on a per column basis in the behaviors editor, there is a column settings collection to specify which column(s) to enable the behavior in. Adding a column setting allows you to enable/disable a particular behavior for a column.

There are 6 different types of column settings, one for each behavior. The column settings are listed below.

When adding column settings, especially in markup, make sure the setting corresponds to the behavior ( e.g, EditingColumnSetting is the setting to use for the Editing behavior ).

Note
Note:

You must add a behavior to WebDataGrid on the control level before you can enable/disable it on the column level.

What You Will Accomplish

You will restrict data editing to only two columns using the behaviors editor of WebDataGrid.

Follow these Steps

  1. Bind WebDataGrid to a SqlDataSource component retrieving data from the Customers table. Only include the columns CustomerID, CompanyName, ContactName, and ContactTitle. For more information on doing this, see Getting Started with WebDataGrid.

  2. In the Microsoft® Visual Studio™ property window, locate the Behaviors property and click the ellipsis (…​) button to launch the Behaviors Editor Dialog.

  3. Check the CellEditing behavior from the list on the left to enable it.

  4. In the properties for Cell Editing, select the ColumnSettings property and click the ellipsis (…) button to launch the column settings designer.

  5. Add 2 Column Setting items for the CustomerID and CompanyName fields. Since these columns, in most cases, should not be edited by the end-user, you will make them read-only.

    1. Set the ColumnKey property of the first column to CustomerID.

      WebDataGrid Setting Column Settings for a Behavior 01.png
    2. Set the ColumnKey property of the second column to CompanyName.

      WebDataGrid Setting Column Settings for a Behavior 02.png
    3. Set both ReadOnly properties to True.

  1. Click Apply then Ok to close the editor.

  2. Click Apply then Ok to close the Behaviors editor.

You can also do the above steps in code.

In Visual Basic:

Me.WebDataGrid1.Behaviors.CreateBehavior(Of EditingCore)()
Me.WebDataGrid1.Behaviors.EditingCore.Behaviors.CreateBehavior(Of CellEditing)()
' Create column settings
Dim settingColumn1 As New EditingColumnSetting()
settingColumn1.ColumnKey = "CustomerID"
settingColumn1.ReadOnly = True
Dim settingColumn2 As New EditingColumnSetting()
settingColumn2.ColumnKey = "CompanyName"
settingColumn2.ReadOnly = True
' Add column settings
Me.WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingColumn1)
Me.WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingColumn2)

In C#:

this.WebDataGrid1.Behaviors.CreateBehavior<EditingCore>();
this.WebDataGrid1.Behaviors.EditingCore.Behaviors.CreateBehavior<CellEditing>();
// Create column settings
EditingColumnSetting settingColumn1 = new EditingColumnSetting();
settingColumn1.ColumnKey = "CustomerID";
settingColumn1.ReadOnly = true;
EditingColumnSetting settingColumn2 = new EditingColumnSetting();
settingColumn2.ColumnKey = "CompanyName";
settingColumn2.ReadOnly = true;
// Add column settings
this.WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingColumn1);
this.WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingColumn2);
  1. Run the application. The end-user is only allowed to edit the Contactname and ContactTitle columns.