Freigeben über


DataGrid.IsReadOnly Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a value that indicates whether the user can edit the values in the control.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Data (in System.Windows.Controls.Data.dll)

Syntax

'Declaration
Public Property IsReadOnly As Boolean
public bool IsReadOnly { get; set; }
<sdk:DataGrid IsReadOnly="bool"/>

Property Value

Type: System.Boolean
true if the control is read-only; otherwise, false. The default is false.

Exceptions

Exception Condition
InvalidOperationException

When setting this property to true while the control is in editing mode, the edit cannot be committed or reverted.

Remarks

Dependency property identifier field: IsReadOnlyProperty

If the IsReadOnly property is set to true, none of the cells can enter editing mode.

You can set this behavior for individual columns by setting the DataGridColumn.IsReadOnly property. If the DataGridColumn.IsReadOnly property and the DataGrid.IsReadOnly property are both set, a value of true will take precedence over a value of false.

Examples

The following code example demonstrates how to set the IsReadOnly property in code. This example is part of a larger example available in the Walkthrough: Customizing the DataGrid Control Using Properties topic.

Private Sub cbReadOnly_Changed(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim cb As CheckBox = sender
    If Me.dataGrid1 IsNot Nothing Then
        Me.dataGrid1.IsReadOnly = cb.IsChecked
    End If
End Sub
private void cbReadOnly_Changed(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (this.dataGrid1 != null)
        this.dataGrid1.IsReadOnly = (bool)cb.IsChecked;
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources