Jordan Jones
Jordan Jones

Reputation: 125

''EditItem' is not allowed for this view.' when trying to edit a item in the DataGrid

So when I doubleclick an item to edit the value in my datagrid I keep getting the error message.

''EditItem' is not allowed for this view.'

And it looks like this enter image description here

I've never encountered that before so I am not sure what way to go to deal with this. What is causing this issue and what is the correct way of dealing with such an issue so I know how to deal with it in the future. I tried looking into it on Google but it all had with lists to do and since I am not using a list I couldnt see the connection to my application.

XAML

    <DataGrid Name="dgItems">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Property" Binding="{Binding Property}" />
            <DataGridTextColumn Header="Value" Binding="{Binding Value}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

CS

private void btnStart_Click(object sender, RoutedEventArgs e)
{
    string path = "";
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Properties | *.properties";
    if (ofd.ShowDialog() == true)
    {
        path = ofd.FileName;
    }
    using (StreamReader sr = new StreamReader(path))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            if (!line.StartsWith("#"))
            {
                string[] lines = line.Split('=');
                string property = lines[0];
                string value = lines[1];
                this.dgItems.Items.Add(new ServerProperties { Property = property, Value = value });
                Debug.Print($"Property: {property} Value: {value}");
            }
        }
    }
}

My Class that gets & sets the values

public class ServerProperties
{
    public string Property { get; set; }
    public string Value { get; set; }
}

Upvotes: 1

Views: 1915

Answers (1)

mm8
mm8

Reputation: 169400

You should set the ItemsSource property of the DataGrid to a collection that implements the IList interface for you to be able to edit the items:

var list = new List<ServerProperties> { ... };
dgItems.ItemsSource = list;

Don't add any items directly to the Items property of the DataGrid:

dgItems.Items.Add(new ServerProperties());

So you should modify your code slightly:

private void btnStart_Click(object sender, RoutedEventArgs e)
{
    string path = "";
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Properties | *.properties";
    if (ofd.ShowDialog() == true)
    {
        path = ofd.FileName;
    }
    List<ServerProperties> serverProperties = new List<ServerProperties>();
    using (StreamReader sr = new StreamReader(path))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            if (!line.StartsWith("#"))
            {
                string[] lines = line.Split('=');
                string property = lines[0];
                string value = lines[1];
                serverProperties.Add(new ServerProperties { Property = property, Value = value });
                Debug.Print($"Property: {property} Value: {value}");
            }
        }
    }
    dgItems.ItemsSource = serverProperties;
}

Upvotes: 2

Related Questions