user5636721
user5636721

Reputation:

C# WPF Datagrid(DataTable) set name with a string variable

I try to set up a Datagrid in wpf. To fill it out I use a DataTable. The problem is that if I want to create a new Column, I can't set the name from a List of string variables, because the Coloumn header will be wrong placed and I can't add values to the rows under the Column. In code:

datatable.Columns.Add(new DataColumn("Test", typeof(string)));//This works

datatable.Columns.Add(new DataColumn(stringlist[i], typeof(string)));//This doesn't work

This illustration shows the wrong placement:

enter image description here

Upvotes: 0

Views: 452

Answers (1)

devuxer
devuxer

Reputation: 42384

I would suggest you trim any white space that the user may accidentally enter before/after the name, like this:

datatable.Columns.Add(new DataColumn(stringlist[i].Trim(), typeof(string)));

Upvotes: 1

Related Questions