wardh
wardh

Reputation: 479

Trouble making columns read-only in silverlight

Hi I'm having a bit trouble with locking my columns in my datagrid in silverlight.

void proxy_ListAllGroupsCompleted(object sender, gkws.ListAllGroupsCompletedEventArgs e)
    {
        grouplist = e.Result;

        List<allGroups> source = new List<allGroups>();


        for (int i = 0; i < grouplist[0].Count; i++)
        {
            source.Add(new allGroups()
            {

                ID = Convert.ToInt32(grouplist[0][i]),
                Name = grouplist[1][i],
                CreationDate = grouplist[2][i],
                Creator = grouplist[3][i]
            });
        }

        mainGroupDG.ItemsSource = source;

        mainGroupDG.Columns[0].IsReadOnly = true;
        mainGroupDG.Columns[2].IsReadOnly = true;
        mainGroupDG.Columns[3].IsReadOnly = true;
    }

When I debug I get the error "Index was out of range". Although my datagrid auto-generates the column before I try to lock them.

Thanks for the help.

Wardh

Upvotes: 2

Views: 1054

Answers (1)

Elad Katz
Elad Katz

Reputation: 7591

The problem is that when you're setting the IsReadOnly, the columns have yet to be created. What you need to do is to catch an event from the DataGrid that happens AFTER the columns have been created. for example, you could do this:

    private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        e.Column.IsReadOnly = true;
    }

Upvotes: 3

Related Questions