Freigeben über


Silverlight 3.0 Datagrid - How to change a cell state?

Hi Syam Pinnaka, Sr. SDE in Infosec tools team.

Silverlight 3.0 datagrid can be used to bind to any enumerable collection and display the data in the grid. The data changes in the grid can be propagated back to the bound data using a special type in silverlight called ObservableCollection. We will discuss more about ObservableCollection in a separate post. In this post Lets see how to change a datagrid cell state based on certain condition. For example lets say there are two DataGridCheckBoxColumn columns and first check box column state will need to change to read-only based on the value of second check box column.

We can accomplish this by handling datagrid events like BeginningEdit or CellEditEnded. In our example, we can use BeginningEdit to check for checkbox whether the checkbox being clicked is first one, if so check the state of second check box to allow the click or not. Example code below.

#region selectUsersGrid_BeginningEdit
private void selectUsersGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    if (e.Column.DisplayIndex == 0) //First DataGridCheckBoxColumn
    {
        User u = e.Row.DataContext as User; //fetch the row data.
        if (u.IsMember == false) //examine the second checkbox data, do not allow if its false
        {
            e.Cancel = true;
        }
    }
}
#endregion

The same effect can be accomplished in some other ways. For example we can use CellEditEnded instead of BeginningEdit. In CellEditEnded, check for second check box state and mark first one as read-only when required. Example code below.

#region selectUsersGrid_CellEditEnded
private void selectUsersGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
{
    if (e.Column.DisplayIndex == 1) //Second check box state changed.
    {
        User u = e.Row.DataContext as User; //fetch the row data
        if (u.IsMember == false) //This is not a member, Clear IsDeny (make first check box as read-only)
            u.IsDeny = false;
    }
}
#endregion

One point to note in the above two code snippets is that, we are modifying the data (binding) to alter the cell state instead of cell itself. This becomes essential when we waned to change state that is not related to data, for example lets say background color of the cell. this can be accomplished as below.

 

#region selectUsersGrid_CellEditEnded
private void selectUsersGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
{
    if (e.Column.DisplayIndex == 1) //Second check box state changed.
    {

FrameworkElement firstCheckbox = e.Column.GetCellContent(e.Row);
if (firstCheckbox is CheckBox)
{
    CheckBox c = firstCheckbox as CheckBox;
    c.Background = new SolidColorBrush(Colors.Red);
}

    }
}
#endregion

 

This is about it for now, We will talk more silverlight during coming posts. Feel free to contact me at syamp@microsoft.com if you have any questions about the above post.

Happy coding!