![WebDataGrid Setting Column Settings for a Behavior 01.png](images/WebDataGrid_Setting_Column_Settings_for_a_Behavior_01.png)
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 ).
You will restrict data editing to only two columns using the behaviors editor of WebDataGrid.
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.
In the Microsoft® Visual Studio™ property window, locate the Behaviors property and click the ellipsis (…) button to launch the Behaviors Editor Dialog.
Check the CellEditing behavior from the list on the left to enable it.
In the properties for Cell Editing, select the ColumnSettings property and click the ellipsis (…) button to launch the column settings designer.
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.
Set the ColumnKey property of the first column to CustomerID.
Set the ColumnKey property of the second column to CompanyName.
Set both ReadOnly properties to True.
Click Apply then Ok to close the editor.
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);
Run the application. The end-user is only allowed to edit the Contactname and ContactTitle columns.