0

I am adding rows to a data grid dynamically using a list as and item source.

However I want to disable the user for editing some of the cells of the data grid.

How can I do that in a simple way?

Attached my code:

  /// <summary>
  /// this class contain the data for the table in order to add the data for the table
  /// </summary>
  public class DataFroGrid
  {
        private string des;
        /// <summary>
        /// conatin the desction field for the data
        /// </summary>
        public string Description
        {
            get { return des; }
            set { des = value; }
        }
        private string numberOfBytes;
        /// <summary>
        /// contain the number of byte for adding to the data
        /// </summary>
        public string NumberOfBytes
        {
            get { return numberOfBytes; }
            set { numberOfBytes = value; }
        }

        private string value;

        /// <summary>
        /// contain the value for adding to the data
        /// </summary>
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public DataFroGrid ()
        {
            des = "";
            numberOfBytes = "";
            value = "";
        }
    }

    private List<DataFroGrid> _ListForDataCommands; // a list for attached the data as a data source

    public addQuestionMarkCommand(string[] description, int[] numberOfBytes ,string [] Value)
    {
        WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; // start window in the middle of the screen
        _ListForDataCommands = new List<DataFroGrid>();
        res = ""; // get the result values
        InitializeComponent();            
        eUserClosed += addQuestionMarkCommand_eUserClosed; // create an event for closing the window.

        // update the item source per the data that has been received
        for (int i = 0; i < description.Length; i++)
        {
            DataFroGrid dfg = new DataFroGrid();
            dfg.Description = description[i];
            dfg.NumberOfBytes = numberOfBytes[i].ToString();
            dfg.Value = Value[i];
            _ListForDataCommands.Add(dfg);                
            //want to disable editing cell per data????
        }

        dataGridCommand.ItemsSource = _ListForDataCommands;                                            
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Barak Rosenfeld
  • 394
  • 5
  • 18
  • It would be useful to see your xaml. Please check this so post for some other ideas: http://stackoverflow.com/questions/3843331/how-to-make-wpf-datagridcell-readonly – Taterhead Mar 20 '16 at 15:04

2 Answers2

0

If you want a code-behind solution, there is a IsReadOnly property on the DataGridColumn and you can set that in an event handler for DataGrid.AutoGeneratingColumn event. Here is an MSDN link for AutoGeneratingColumn event that may be helpful.

Here's a code snippet that will make the NumberOfBytes column read-only:

private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Header.ToString() == "NumberOfBytes")
    {                
         e.Column.IsReadOnly = true; // Makes the column as read only
    }
} 
Steve Wong
  • 2,038
  • 16
  • 23
0

DataGridCell has property IsEnabled. You may override CellStyle and add binding to IsEnabled property.

<DataGrid>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Converter={local:IsEnabledConverter}}"  />
            </Style>
        </DataGrid.CellStyle>
</DataGrid>

Decision should be made in IsEnabledConverter:

public class IsEnabledConverter : MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var cell = value as DataGridCell;
        var rowData = cell.DataContext as DataFroGrid; // data object bound to the row

        // you may analyze column info, row data here and make a decision

        if (cell.Column.Header.ToString()=="ColumnName1")
        {                
            return false;
        }

        return true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}
nicolas2008
  • 945
  • 9
  • 11