Silverlight DataGrid Exception Column Column Overrides

I am trying to set the initial display order of column headers in a silverlight datagrid by changing the DisplayIndex values โ€‹โ€‹of the column header. If I try to set the order of the columns during page load, I get an out of range exception. If I set the order of the columns (same procedure) at a later time, for example in the button click handler, it works. Is this just a bug in the silverlight datagrid control? Suggestions for a possible job?

+1


source to share


3 answers


I am guessing that you are having a problem modifying the DisplayIndex of the columns in the DataGrid from the Load event, as they have not been created yet. You don't say, but I am assuming you get a DataGrid to create your columns automatically, as otherwise you can just set DisplayIndex in your XAML when defining DataGrid columns.

When the DataGrid generates columns, it fires an AutoGeneratingColumn event for each column, allowing you to change its properties. This is a little rough, but one solution might be to set DisplayIndex to a handler for this event using the PropertyName for which the column is being created.



private void grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    switch (e.PropertyName)
    {
        case "Name":
            e.Column.DisplayIndex = 1;
            break;

        case "Age":
            e.Column.DisplayIndex = 0;
            break;
    }
}

      

+1


source


actually you need to subscribe to the grid.Loaded event and reorder the colums there:

public UserManagementControl()
    {
        InitializeComponent();
        dataGridUsers.Loaded += new RoutedEventHandler(dataGridUsers_Loaded);
    }

    void dataGridUsers_Loaded(object sender, RoutedEventArgs e)
    {
        dataGridUsers.Columns[0].DisplayIndex = 1;
    }

      



You have an ArgumentOutOfRangeException because the control has not been loaded yet

0


source


/// <summary>
/// Automation DataGrid Control - Columns Localization and Ordering
/// Option1: Localization of Columns Automatically
/// Option2: Ordering columns in DataGrid Automatically
/// </summary>
/// <param name="dataGrid"> DataGrid control</param>
/// <param name="ContractType"> Contract of Row DataItem 
/// Example: typeof(ClientType) 
/// </param>
/// <param name="columns"> Ordered Properties of Contract
/// Example:  columns = "Id_Client,Client,GeographyItem,Flag_Approved,ClientType,ClientRelation,ClientPrestigeLevel"
/// </param>
public void AutomateDataGridColumns(DataGrid dataGrid, Type Contract, String columns)
{
    try
    {   
        List<String> OrderedColumns = columns.Split(new string[] { ",", "|", ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();

        //Buid Order of created COLUMNS
        dataGrid.Loaded += (sndr, arg) =>
        {
            if (dataGrid.Columns.Count == OrderedColumns.Count && dataGrid.AutoGenerateColumns == true)                       
            {
                foreach (var item in dataGrid.Columns)
                {
                    Int32 displayIndex = OrderedColumns.IndexOf(item.Header.ToString());
                    if (displayIndex != -1)
                    {  item.DisplayIndex = displayIndex;  }                          
                }
            };
        };

        //DataGridColumn Localization  
        dataGrid.AutoGeneratingColumn += (sndr, arg) =>
        {
                LocalizeDataGridColumn(sndr as DataGrid, arg, Contract, OrderedColumns);

                //We need To Update DataGrid  after last Column Localized -->so  Loaded event will be Raised/
                // or ArgumentOutOfRange Exception will be thrown
                if (dataGrid.Columns.Count == OrderedColumns.Count && dataGrid.AutoGenerateColumns == true)
                {
                    dataGrid.UpdateLayout();
                }
        };

    }           
    catch (Exception exc)
    {  throw exc;
    }
}



/// <summary>
/// DataGridColumn Control Localization
/// </summary>
/// <param name="dataGrid">Host DataGrid control </param>
/// <param name="arg">Auto Generated Column arg </param>
/// <param name="Contract">Type Contract</param>
/// <param name="localizationColumns">Ordered Properties to Contract </param>
protected void LocalizeDataGridColumn(DataGrid dataGrid, DataGridAutoGeneratingColumnEventArgs arg, Type Contract, List<String> localizationColumns)
{
    try
    {
        DataGridColumn Column = arg.Column;

        if (localizationColumns.Contains(Column.Header.ToString()))
        {
            // LOCALIZING Column.Header                      

            // Check column local resource key exist 
            // CultureKeys -  local Culture enum type 
            // SystemDispatcher - is My SL4  MEF Bootstrappper 
            // LocalizationService  - is My Localization service in SL4   
            // if somebody is interested i can share more of my LocalizationService - use mail      
            CultureKeys currntCulture = SystemDispatcher.LocalizationService.CurrentCulture;
            string ResourceKey = LocalResKeys.BoPropElmNameLoc.ToString() + "\\" + Contract.Name + @"|" + Column.Header.ToString();

            if (SystemDispatcher.LocalizationService.CultureResources[currntCulture].Item2.ContainsKey(ResourceKey))
                Column.Header = SystemDispatcher.LocalizationService.CultureResources[currntCulture].Item2[ResourceKey];                                       
        }
        else
        {    arg.Cancel = true;
        }
    }
    catch (Exception exc)
    {
        throw exc;
    }

}

      

0


source







All Articles