AustinT
AustinT

Reputation: 2026

How exactly would I make certain DataGrid columns readonly programatically?

I am having troubles making certain datagrid columns readonly.

XML

<DataGrid x:Name="fieldsDataGrid" AutoGenerateColumns="true"></DataGrid>

C# Code

List<Field> fields = TestingClass.generateTestFields() // Generates the list
fieldsDataGrid.ItemSource = fields; 

// I tried both ways
FieldsDataGrid.Columns[0].isReadOnly = true;
FieldsDataGrid.Columns["Tokens"].isReadOnly = true; // Tokens is the column name

What I have attmpted FieldsDataGrid.Columns[0].isReadOnly = true; In this case I ended up getting a index was out of range. It seems like when I debugged it Columns was empty? Why is it empty when I called fieldsDataGrid.ItemSource = fields

If this is the incorrect way to implement this, please suggest a better option!

Thank you for the help!

edit

I have added in the XML AutoGeneratedColumns = "true" - I am still having the same error.

Upvotes: 1

Views: 853

Answers (1)

dotNET
dotNET

Reputation: 35400

If you are autoGenerating columns, then use AutoGeneratingColumn event and set column's readonly property there. If you're adding columns manually, IsReadOnly property should be accessible directly through column name at the point of creation.

Upvotes: 4

Related Questions