Hello,
I'm having an issue where the column header is missing when you add a column to the grid after it has initially been created. I have created a sample project that demonstrates my issue, but it appears I can't attach it so I included the code at the end of this post. To reproduce it, just click on the 'Add Column' button. Am I doing something wrong in my code or is this a bug?
In my example I'm also calling Rebind() and UpdateLayout() on the grid after adding the column because if I don't then no data appears in the column, though the column itself is added.
Note: I borrowed the Custom and Order classes from a sample app on this forum.
Thanks,
Joel
Window1.xaml
Window1.cs
I'm having an issue where the column header is missing when you add a column to the grid after it has initially been created. I have created a sample project that demonstrates my issue, but it appears I can't attach it so I included the code at the end of this post. To reproduce it, just click on the 'Add Column' button. Am I doing something wrong in my code or is this a bug?
In my example I'm also calling Rebind() and UpdateLayout() on the grid after adding the column because if I don't then no data appears in the column, though the column itself is added.
Note: I borrowed the Custom and Order classes from a sample app on this forum.
Thanks,
Joel
Window1.xaml
<Window x:Class="AddColumnIssue.Window1" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" |
Title="Window1" Height="380" Width="701"> |
<Grid> |
<Grid.RowDefinitions> |
<RowDefinition Height="30" /> |
<RowDefinition Height="*" /> |
</Grid.RowDefinitions> |
<Button x:Name="AddColumnButton" Grid.Row="0" Height="23" Width="75" Click="AddColumnButton_Click" >Add Column</Button> |
<telerik:RadGridView x:Name="ItemsGrid" Grid.Row="1" ItemsSource="{Binding}" AutoGenerateColumns="False" /> |
</Grid> |
</Window> |
Window1.cs
public partial class Window1 : Window |
{ |
public Window1() |
{ |
InitializeComponent(); |
AddColumns(); |
this.DataContext = BuildCustomers(); |
} |
private void AddColumnButton_Click(object sender, RoutedEventArgs e) |
{ |
AddColumn(typeof(string), "Email", 200); |
RefreshGrid(); |
} |
private void AddColumns() |
{ |
AddColumn(typeof(int), "Number", 100); |
AddColumn(typeof(string), "Name", 100); |
} |
private void AddColumn(Type dataType, string name, int width) |
{ |
GridViewDataColumn column = new GridViewDataColumn(); |
column.DataType = dataType; |
column.HeaderText = name; |
column.UniqueName = name; |
column.Width = new GridLength(width); |
column.IsReadOnly = true; |
ItemsGrid.Columns.Add(column); |
} |
private void RefreshGrid() |
{ |
ItemsGrid.Rebind(); |
ItemsGrid.UpdateLayout(); |
} |
private static ObservableCollection<Customer> BuildCustomers() |
{ |
ObservableCollection<Customer> customers = new ObservableCollection<Customer>(); |
ObservableCollection<Order> orders = new ObservableCollection<Order>(); |
for (int i = 0; i < 10; i++) |
orders.Add(new Order { Number = i, Details = "Order" + i }); |
for (int i = 0; i < 100; i++) |
customers.Add(new Customer { Email = "Mail" + i, Name = "Name" + i, Number = i, Orders = orders }); |
return customers; |
} |
} |