1

I'm having a rough time deleting a column of data in my datagrid and having that deletion propagate to my data source. I've implemented the solution for binding data found in this SO thread, and am using this code to delete my column:

vizDataGrid.Columns.Remove(cell.Column);

What's happening is that the UI shows the column being deleted, but when I go add a column, the column I thought I had just deleted appears (or at least the data that was inside that column does). Any help would be greatly appreciated, I've been at this for days (WinForms was so much simpler!). Thanks!

4
  • why not just hide the column? Commented Jul 31, 2013 at 5:25
  • Because then the data it represents isn't actually deleted, right? Commented Jul 31, 2013 at 5:28
  • The data structure you design (in fact includes a collection of columns) should be fixed, your grid just shows the columns user wants to interact with. Commented Jul 31, 2013 at 5:36
  • The issue is that my data is dynamic and I won't know how many columns I may or may not have (CSV file). Commented Jul 31, 2013 at 5:44

1 Answer 1

0

It is because of AutoGenerateColumns="True". Set it false and add columns on your own in code behind.

Please check this SO post to dynamically adding columns to a DataGrid.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok awesome, thanks. One thing real quick though - how would I go about handling adding rows? Could I still use the Bindable Dynamic Dictionary or would I have to do something else? I have dynamic data (CSV file) and won't know how many columns I have so I'm limited by that.
If you check the link I provided in answer, you can see how to find number of columns from your data source. Regarding to rows, you just bind your data source to ItemsSource property of DataGrid and WPF will generate them for you. If you want to add another row then create a new data item and add it your data source.
I understand that, I'm building out the columns right now by parsing the CSV and counting the number of items on a line. For rows, I thought I'd iterate over each line and create a DataGridRow object. I'm stuck at the part where I populate the row with the appropriate data from the specific line in my CSV file (represented as an array in my code right now). So I think my issue is how do I bind my data source to the ItemsSource property?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.