DataGrid.PreparingCellForEdit Event in silverlight


Introduction :

DataGrid.PreparingCellForEdit Event.This event occurs when cell in a DataGridTemplateColumn enters editing mode.

Explanation : public event EventHandler<DataGridPreparingCellForEditEventArgs> PreparingCellForEdit
PreparingCellForEdit event is useful when we want to customize/change the behaviour of the editing element based on some condition.

On PreparingCellForEdit we can make datagrid readonly

dataGridFirst.IsReadOnly = true;
we can get the selected row elements.we can edit those element by making datagrid
readonly false.

 void datagrid1_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {          
            Person p = e.Row.DataContext as Person;
            if (p.ID % 2 == 0)
            {
                if (e.Column.DisplayIndex == 1)
                {
                    TextBox textBox = (e.EditingElement as TextBox);
                    textBox.IsReadOnly = true;
                    textBox.Background = new SolidColorBrush(Colors.Gray);                  
                }
            }
        }

        void dataGridFirst_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            dataGridFirst.IsReadOnly = true;
            ClientsList selectedrow = (ClientsList)dataGridFirst.SelectedItem;
            Clientname = selectedrow.Name;
            Clientid = selectedrow.ClientID;
            dataGridFirst.IsReadOnly = false;
           
        }
Ebook Download
View all
Learn
View all