Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Possible DataGrid.Columns.Clear() bug? RSS

4 replies

Last post Sep 02, 2011 08:59 AM by SteveWong

(0)
  • Telos

    Telos

    Member

    331 Points

    125 Posts

    Possible DataGrid.Columns.Clear() bug?

    Jul 23, 2009 12:29 PM | LINK

    I'm not sure if this is a bug or not but it's strange at least. I have a simple demo application which shows this bug(?). There is a datagrid where I generate columns dynamically. If I clear the columns with Clear() and then generate them again, I get an ArgumentOutOfRangeException when adding a column (Column.Add(column)). If I remove the columns manually (for loop and RemoveAt), I don't get an exception. This only happens when I have grouping on.

     

    Here's the code for the application:

    C#
     

    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    namespace DataGridColumnsClearBug
    {
    public partial class MainPage : UserControl
    {
    public MainPage()
    {
    InitializeComponent();

    GenerateColumns();
    GenerateData();
    }

    private void GenerateColumns()
    {
    for (int i = 0; i < 2; i++)
    {
    DataGridTextColumn column = new DataGridTextColumn();
    column.Header = "column #" + i.ToString();
    column.Binding = new Binding { Path = new PropertyPath("c" + i.ToString()) };
    bugGrid.Columns.Add(column);
    }
    }

    private void GenerateData()
    {
    ObservableCollection items = new ObservableCollection();
    BugItem bugItem;
    bugItem = new BugItem() { c0 = "c0", c1 = "c1" };
    items.Add(bugItem);

    PagedCollectionView view = new PagedCollectionView(items);
    view.GroupDescriptions.Add(new PropertyGroupDescription("c1"));
    bugGrid.ItemsSource = view;
    }

    private void GenerateBugButton_Click(object sender, RoutedEventArgs e)
    {
    // This creates bug
    // ---------------------------------------

    bugGrid.Columns.Clear();

    // This doesn't
    // ---------------------------------------
    //int columnCount = bugGrid.Columns.Count;
    //for (int i = 0; i < columnCount; i++)
    //{
    // bugGrid.Columns.RemoveAt(0);
    //}

    // -----------------------


    GenerateColumns();
    }
    }

    public class BugItem
    {
    public string c0 { get; set; }
    public string c1 { get; set; }
    }
    }
     
    XAML
     
    <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="DataGridColumnsClearBug.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
    <StackPanel Orientation="Vertical">
    <data:DataGrid x:Name="bugGrid" AutoGenerateColumns="False"></data:DataGrid>

    <Button x:Name="GenerateBugButton" Content="Bug Generator" Click="GenerateBugButton_Click"></Button>
    </StackPanel>
    </Grid>
    </UserControl>
     

    Datagrid columns clear grouping

    - Antti Simonen
    Follow me on Twitter
  • jbendler

    jbendler

    Member

    2 Points

    1 Post

    Re: Possible DataGrid.Columns.Clear() bug?

    Mar 02, 2010 01:12 PM | LINK

    I am having a very similiar issue.  When I use the Clear() method - I can recreate the columns; however, I cannot recreate the event handles for Edit and Delete.

     

    private void DisplayExistingData()

    {

    System.Web.UI.WebControls.
    BoundColumn boundColumn;

    System.Web.UI.WebControls.ButtonColumn buttonColumn;

    SqlDataAdapter sqlDataAdapter;

    DataSet ds;

    ds = new DataSet();

    RecordDisplay.Columns.Clear();

    // The following 2 lines of code have no affect

    RecordDisplay.EditCommand +=
    new DataGridCommandEventHandler(RecordDisplay_EditCommand);

    RecordDisplay.DeleteCommand += new DataGridCommandEventHandler(RecordDisplay_DeleteCommand);

    buttonColumn = new ButtonColumn();

    buttonColumn.ButtonType = ButtonColumnType.PushButton;

    buttonColumn.CommandName = "Edit";buttonColumn.Text = "Edit";

    RecordDisplay.Columns.Add(buttonColumn);

    buttonColumn = new ButtonColumn();

    buttonColumn.ButtonType = ButtonColumnType.PushButton;

    buttonColumn.CommandName = "Delete";

    buttonColumn.Text = "Delete";

    RecordDisplay.Columns.Add(buttonColumn);

    sqlDataAdapter =
    new SqlDataAdapter("select * from " + TableSelection.Text, DAL.Properties.Settings.Default.RateMatrixConnectionString);

    sqlDataAdapter.Fill(ds);

    for (int index = 0; index < ds.Tables[0].Columns.Count; index++)

    {

    boundColumn =
    new System.Web.UI.WebControls.BoundColumn();

    boundColumn.DataField = ds.Tables[0].Columns[index].ColumnName;

    boundColumn.HeaderText = boundColumn.DataField;

    RecordDisplay.Columns.Add(boundColumn);

    }

    if (ds.Tables[0].Rows.Count > 0)

    {

    RecordDisplay.DataSource = ds.Tables[0];

    RecordDisplay.DataBind();

    }

    RecordDisplay.Columns[2].Visible =
    false;

    }

  • seriousme

    seriousme

    Member

    21 Points

    20 Posts

    Re: Possible DataGrid.Columns.Clear() bug?

    Jul 15, 2010 03:11 PM | LINK

    Ok, so after more than one year this bug is still alive and healthy!


    This won't work :

    DataGrid.Columns.Clear();
    DataGrid.Columns.Add(...);

    This fix, thanks a lot Telos, will work :

    while (DataGrid.Columns.Count != 0)
    {
    	DataGrid.Columns.RemoveAt(0);
    }
    DataGrid.Columns.Add(...);

    The exception is raised from the "Add" or "Insert" methods;

    so it seems like the "Clear" method leaves the Columns (or anything else on which it depends) in a bad state.


    Does a bug report exist for this bug ?

    If not, where could I report it in order for the Silverlight team to correct it ?


    Thanks in advance.

    Bug DataGrid Clear RemoveAt Fix

  • thierry38

    thierry38

    Member

    2 Points

    1 Post

    Re: Possible DataGrid.Columns.Clear() bug?

    May 03, 2011 02:15 PM | LINK

    Hi,

    After spending 4 hours to try to understand why my DataGrid crashed, I found this topic !...
    In my case (SL4), the DataGrid.Columns.Add crash each time I try to add columns to a DataGrid that was grouped before loading.

    My scenario :
    1. I load datas into my DataGrid (via a PagedCollectionView in MVVM) and add columns to the DataGrid by code -> OK
    2. I add a GroupDescription to the PagedCollectionView and submit change to the DataGrid, thus the DataGrid reloads data with the column grouped -> OK
    3. then I reload data from the database, clear columns and try to add columns to the DataGrid -> crash

    With your loop, it's ok, it doesn't crash anymore.

    If this scenario can help to solve this bug, which seems to be a real one !...

  • SteveWong

    SteveWong

    Contributor

    6769 Points

    1351 Posts

    Re: Possible DataGrid.Columns.Clear() bug?

    Sep 02, 2011 08:59 AM | LINK

    I have finally found a workaround for this case.

    First, let me define what the bug is.

    Try this:

    1. Add Columns to

    2. Get a bunch of data and load it into DataGrid with PagedCollectionView

    3. Clear the Columns and Add the Columns again

    4. Get another Bunch of data and load it into Datagrid with PagedCollectionView

     

    You will find you cant process to step 4 and error occurs OutOfRange

     

    Workaround is simple.

    Before step3, which is before columns clearing and adding, clear the GroupDescriptions in PagedCollectionView

    i.e. pcv.GroupDescriptions.Clear()

     

    Hope it helps anyone which is stuck on it before or now

    Regards,
    SteveWong (HongKong)
    Please mark post as answer if they help you