We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Index was out of range error assigning GridComboBoxColumn.Width

Hi,

Assigning a value to GridComboBoxColumn.Width throw an exception, but I'm not able to replicate the issue on a fresh project.

Screenshot 2022-10-25 130943.png



Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index


at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at Syncfusion.UI.Xaml.ScrollAxis.SortedRangeValueList`1.Split(Int32 index, Int32 n)
at Syncfusion.UI.Xaml.ScrollAxis.SortedRangeValueList`1.Split(Int32 index)
at Syncfusion.UI.Xaml.ScrollAxis.SortedRangeValueList`1.RemoveHelper(Int32 removeAt, Int32 count, SortedRangeValueList`1 moveRanges)
at Syncfusion.UI.Xaml.ScrollAxis.SortedRangeValueList`1.SetRange(Int32 index, Int32 count, T value)
at Syncfusion.UI.Xaml.ScrollAxis.LineSizeCollection.SetHidden(Int32 from, Int32 to, Boolean hide)
at Syncfusion.UI.Xaml.Grid.GridColumnSizer.OnColumnPropertyChanged(GridColumn column, String propertyName)
at Syncfusion.UI.Xaml.Grid.GridColumn.OnWidthChanged(DependencyPropertyChangedEventArgs e)
at Syncfusion.UI.Xaml.Grid.GridColumnBase.OnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at Syncfusion.UI.Xaml.Grid.GridPropertyMetadata.OnDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at Syncfusion.UI.Xaml.Grid.GridPropertyMetadata.OnInternalDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.Freezable.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at Syncfusion.UI.Xaml.Grid.GridColumnBase.set_Width(Double value)




3 Replies 1 reply marked as answer

SS Sethuramkumar Senthilkumar Syncfusion Team October 26, 2022 02:42 PM UTC

Hi Gregory Amable Garcia,

Provided details are insufficient to find the exact cause for the issue. Can you please provide the more information related to your query?

Can you please share us below things?      

  1. Provide the code snippet related to the customization of SfDataGrid both Code behind and XAML code snippet.
  2. Confirm whether did you perform any column manipulation operations apart from setting width of the column.  
  3. Video illustration of the reported issue.

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Sethuramkumar S



GA Gregory Amable Garcia October 26, 2022 03:30 PM UTC

  1.               x:Name="ProxyElement"
    DataContext="{Binding}"
    Visibility="Collapsed" />
    Name="DataGridSourceTypes"
    Grid.Row="0"
    Grid.RowSpan="3"
    Grid.Column="0"
    Margin="10,0,10,10"
    AllowEditing="True"
    AllowSorting="False"
    ColumnSizer="Auto"
    EditTrigger="OnTap"
    ItemsSource="{Binding Model.BookPages}"
    LiveDataUpdateMode="AllowDataShaping"
    SelectionMode="Single"
    Tag="NoColorGrid">




    DisplayMemberPath="Name"
    HeaderText="Source Type"
    ItemsSource="{Binding DataContext.Model.SourceTypes, Source={x:Reference ProxyElement}}"
    MappingName="BookPageSourceTypeId"
    SelectedValuePath="BookPageSourceTypeId" />

    ColumnSizer="Star"
    HeaderText="Page"
    MappingName="Page" />


    
  2. No other column manipulation, only width property.
  3. Gif attached.
Thanks


Attachment: Error_SFGrid_3f3ebdb6.7z



VS Vijayarasan Sivanandham Syncfusion Team October 27, 2022 04:04 PM UTC

Hi Gregory Amable Garcia,

We suspect that you are clearing the columns collection before setting the width of the column. If you are clearing the columns, that is not a recommended approach, instead of clearing the columns collection, you can store the columns in a property and then use it for setting the width of the column. Please refer to the following code snippet.

// You can check the condition based on your scenario

var columnDictionary = sfdatagrid.Columns.ToDictionary(x => x.HeaderText, y => y);

var viewModel = sfdatagrid.DataContext as ViewModel;

 

foreach (var col in sfdatagrid.Columns)

{

    var colum = viewModel.GridColumns.FirstOrDefault(x => x.MappingName == col.MappingName);

    if (colum == null)

    {

        // Instead of clearing the columns collection, you can store it in a property.

        viewModel.GridColumns.Add(col);

    }

}

 

foreach (var columnSetting in viewModel.GridColumns)

{

    if (columnDictionary.TryGetValue(columnSetting.HeaderText, out GridColumn foundColumn))

    {

        // Here set the width

        foundColumn.Width = columnSetting.ActualWidth;

    }

}

 


Please find the sample in the attachment.

If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: ComboBoxColumnsDemo_6029cab6.zip

Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon