Hi,
If I have data with the following columns A, B, and C but would like to display B, C, and A in the xmalWebGrid. How can I do this in the code? Thanks for you help.
So, you'll need to decide what kind of columns you'll need first.
The grid offers a few different column types:TextColumn, CheckboxColumn, HyperlinkColumn, and TemplateColumn.
The first 3 can all be created in the code behind, the fourth, requires a DataTemplate, which can only be defined in xaml. Assuming you define the DataTemplate in xaml though, you can set the ItemTemplate property of that column in the code behind.
Once you decide the type you can use code like the following:
TextColumn col = new TextColumn(){Key="myfieldName"};
Note: a key must defined on every column, and the key needs to map to a property in your data.
Then you can add the columns that you want in the order that you want them.
this.xamWebGrid1.Columns.Add(col);
Note: this can all be done in xaml as well.
Hope this helps,
-SteveZ
Ah! Ok. Now, I understand. What if I have the Autogenerated set to false, how do I add these columns in the code dynamically. Can you please show me a sample? Thanks much!
Are your columns Autogenerated?
If so, then this it's expected behavior, as we clear out all generated columns when a new item source is applied.
Hi Stephen,
Thank you for your suggestion. Actually, it works the first time.
this.DataGrid1.Columns.Remove(colB);
this.DataGrid1.Columns.Insert(0, colB);
this.DataGrid1.Columns.Remove(colA);
this.DataGrid1.Columns.Insert(1, colA);
this.DataGrid1.Columns.Remove(colC);
this.DataGrid1.Columns.Insert(2, colC);
When I click to rebind again, the DataGrid1 shows ColA, ColB, and ColC instead. Is this a bug? Thanks for your help.
You can use the Columns collection to reorder your columns: