Can you remove columns from the Column Chooser

I want to display the column chooser but I want to remove some of the columns that are in the sfdatagrid from the column chooser popup. I want the columns to be displayed in the grid, just not listed in the column chooser popup. is this possible?

9 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team January 15, 2021 06:13 PM UTC

Hi George Busby,

Thanks for contacting Syncfusion support.

Based on provided information we have prepared the sample and attached the video for your reference by adding and removing column in ColumnChooser in SfDatagrid. Please find the sample and video demo from our end in the below link,

Sample Link: https://www.syncfusion.com/downloads/support/forum/161543/ze/Sample2140619007

Video Link: https://www.syncfusion.com/downloads/support/forum/161543/ze/ColumnChooser-1733154720
 
We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S





GB George Busby January 16, 2021 01:12 AM UTC

The video and code did not help. I understand how the chooser works. What I want to do is have a chooser popup  that does not have all of the columns from the sfdatagrid listed. 

For example if I have a sfdatafrid that displays 4 columns ( Text 1, Text 2, Name , Address) and I do not want the user to be able to hide the "Name" column, the chooser popup would come up with a list like this:
Text 1
Text 2
Address

The Name column would not be in the list, but the Name column would be displayed in the sfdatagrid.



MA Mohanram Anbukkarasu Syncfusion Team January 18, 2021 11:15 AM UTC

Hi George, 

Thanks for the update.  

You can achieve your requirement to restrict some columns from displaying in the column chooser by creating custom ColumnChooser and ColumnChooserPopup as shown in the following code example.  

Code example :  

public class ColumnChooserPopupExt : Syncfusion.WinForms.DataGrid.Interactivity.ColumnChooserPopup 
{ 
    private SfDataGrid DataGrid { get; set; } 
    public ColumnChooserPopupExt(SfDataGrid sfDataGrid) : base(sfDataGrid) 
    { 
        this.Controls.Remove(this.ColumnChooser); 
        DataGrid = sfDataGrid; 
        ColumnChooser = new ColumnChooserExt(sfDataGrid); 
 
        ////UnComment this if you want to close the popup when OK button is clicked 
        //ColumnChooser.OKButton.Click += Close_ColumnChooserPopup; 
 
        ColumnChooser.CancelButton.Click += Close_ColumnChooserPopup; 
        ColumnChooser.BorderStyle = BorderStyle.None; 
        ColumnChooser.ColumnChooserLabel.Visible = false; 
        this.Controls.Add(ColumnChooser); 
    } 
 
    private void Close_ColumnChooserPopup(object sender, EventArgs e) 
    { 
        if (this.DataGrid.GetTopLevelParentDataGrid().FindForm() != null) 
            this.DataGrid.GetTopLevelParentDataGrid().FindForm().Focus(); 
        this.Hide(); 
    } 
} 
 
public class ColumnChooserExt : Syncfusion.WinForms.DataGrid.Interactivity.ColumnChooser 
{ 
    /// <summary> 
    /// The grid attached with the column chooser. 
    /// </summary> 
    private SfDataGrid dataGrid; 
 
    /// <summary> 
    /// Initializes a new instance of the sfDataGrid 
    /// </summary> 
    /// <param name="sfDataGrid">The data grid which needs to be attached to the control.</param> 
    public ColumnChooserExt(SfDataGrid sfDataGrid) : base(sfDataGrid) 
    { 
        dataGrid = sfDataGrid; 
        dataGrid.Columns.CollectionChanged += Columns_CollectionChanged; 
    } 
 
    private void Columns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
        this.CheckedListBox.DataSource = dataGrid.Columns.Where(x => x.MappingName != "OrderID" && x.MappingName != "CustomerID"); 
    } 
 
    protected override void AddCheckedListBox() 
    { 
        base.AddCheckedListBox(); 
 
        this.CheckedListBox.DataSource = dataGrid.Columns.Where(x => x.MappingName != "OrderID" && x.MappingName != "CustomerID"); 
    } 
 
    protected override void OnSearchBoxTextChanged(object sender, EventArgs e) 
    { 
        base.OnSearchBoxTextChanged(sender, e); 
 
        List<GridColumn> searchedItems = new List<GridColumn>(); 
        if (!string.IsNullOrEmpty(this.SearchTextBox.Text)) 
        { 
            searchedItems.Clear(); 
            searchedItems = this.dataGrid.Columns.Where(item => (item.HeaderText.ToLower().Contains(this.SearchTextBox.Text.ToLower()) && item.MappingName != "OrderID" && item.MappingName != "CustomerID")).ToList(); 
            this.CheckedListBox.DataSource = null; 
            this.CheckedListBox.DataSource = searchedItems; 
        } 
        else 
            this.CheckedListBox.DataSource = dataGrid.Columns.Where(x => x.MappingName != "OrderID" && x.MappingName != "CustomerID"); 
 
        this.CheckedListBox.Invalidate(); 
 
    } 


We have prepared a sample using the above given code example and it is available in the following link for you reference.  


Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 


Marked as answer

GB George Busby January 19, 2021 03:04 AM UTC

Thank you. I have it working. Since I had more columns that I wanted to remove than I wanted to show , I reversed the logic to use "=" property name instead of "<>" to property name. 

One question. How can I make this more dynamic where I can pass a list of properties to include in the column chooser list instead of having them hardcoded so I can use this on any SFDatagrid?


MA Mohanram Anbukkarasu Syncfusion Team January 20, 2021 10:50 AM UTC

Hi George, 

Thanks for the update.  

You can achieve your requirement to pass list items to be restricted from showing in the column chooser as shown in the following code example.  

Code example :  

public class ColumnChooserPopupExt : Syncfusion.WinForms.DataGrid.Interactivity.ColumnChooserPopup 
{ 
    private SfDataGrid DataGrid { get; set; } 
    public ColumnChooserPopupExt(SfDataGrid sfDataGrid) : base(sfDataGrid) 
    { 
        this.Controls.Remove(this.ColumnChooser); 
        DataGrid = sfDataGrid; 
        ColumnChooser = new ColumnChooserExt(sfDataGrid, new ObservableCollection<string>() { "OrderID", "CustomerID" }); 
 
        ////UnComment this if you want to close the popup when OK button is clicked 
        //ColumnChooser.OKButton.Click += Close_ColumnChooserPopup; 
 
        ColumnChooser.CancelButton.Click += Close_ColumnChooserPopup; 
        ColumnChooser.BorderStyle = BorderStyle.None; 
        ColumnChooser.ColumnChooserLabel.Visible = false; 
        this.Controls.Add(ColumnChooser); 
    } 
 
    private void Close_ColumnChooserPopup(object sender, EventArgs e) 
    { 
        if (this.DataGrid.GetTopLevelParentDataGrid().FindForm() != null) 
            this.DataGrid.GetTopLevelParentDataGrid().FindForm().Focus(); 
        this.Hide(); 
    } 
} 
 
public class ColumnChooserExt : Syncfusion.WinForms.DataGrid.Interactivity.ColumnChooser 
{ 
    /// <summary> 
    /// The grid attached with the column chooser. 
    /// </summary> 
    private SfDataGrid dataGrid; 
 
    private ObservableCollection<string> restrictedColumns; 
    /// <summary> 
    /// Initializes a new instance of the sfDataGrid 
    /// </summary> 
    /// <param name="sfDataGrid">The data grid which needs to be attached to the control.</param> 
    public ColumnChooserExt(SfDataGrid sfDataGrid, ObservableCollection<string> restrictedColumns) : base(sfDataGrid) 
    { 
        dataGrid = sfDataGrid; 
        this.restrictedColumns = restrictedColumns; 
        dataGrid.Columns.CollectionChanged += Columns_CollectionChanged; 
    } 
 
    private void Columns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
        this.CheckedListBox.DataSource = dataGrid.Columns.Where(x => !restrictedColumns.Contains(x.MappingName)); 
    } 
 
    protected override void AddCheckedListBox() 
    { 
        base.AddCheckedListBox(); 
 
        this.CheckedListBox.DataSource = dataGrid.Columns.Where(x => !restrictedColumns.Contains(x.MappingName)); 
    } 
 
    protected override void OnSearchBoxTextChanged(object sender, EventArgs e) 
    { 
        base.OnSearchBoxTextChanged(sender, e); 
 
        List<GridColumn> searchedItems = new List<GridColumn>(); 
        if (!string.IsNullOrEmpty(this.SearchTextBox.Text)) 
        { 
            searchedItems.Clear(); 
            searchedItems = this.dataGrid.Columns.Where(item => (item.HeaderText.ToLower().Contains(this.SearchTextBox.Text.ToLower()) && !restrictedColumns.Contains(item.MappingName))).ToList(); 
            this.CheckedListBox.DataSource = null; 
            this.CheckedListBox.DataSource = searchedItems; 
        } 
        else 
            this.CheckedListBox.DataSource = dataGrid.Columns.Where(x => !restrictedColumns.Contains(x.MappingName)); 
 
        this.CheckedListBox.Invalidate(); 
 
    } 
} 


Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A 



MD Michal Dziubek November 3, 2022 10:03 AM UTC

I tried to adjust this example to my needs. But these not working and I don't know why.


I upgrade to .NET 4.7 and recive


System.NotImplementedException


during app start.



HN Harinath Navaneethakrishnan Syncfusion Team November 9, 2022 04:07 PM UTC

Hi Michal,


We have confirmed the reported scenario is a defect and logged a report for the reported scenario “System.NotImplementedException is thrown while running the SfDataGrid sample”. We will include the fix in our Weekly NuGet release which is scheduled on 29th November 2022.


You can track the status of this defect using the following feedback link:


https://www.syncfusion.com/feedback/38980/system-notimplementedexception-is-thrown-while-running-the-sfdatagrid-sample



If you have any more specification replication procedures or a scenario to be tested, you can add it as a comment in the portal.


Please let us know if you need any further assistance


Note: The provided feedback link is private, and you need to log in to view this feedback.



HN Harinath Navaneethakrishnan Syncfusion Team November 29, 2022 02:35 PM UTC

Sorry for the inconvenience,


We have fixed the reported issue but due to some internal issues, we are not able to include this fix in the recently released NuGet. We will include the fix in the next NuGet release which is scheduled for December 6th, 2022.



HN Harinath Navaneethakrishnan Syncfusion Team December 7, 2022 11:53 AM UTC

We have included the fix in our latest Weekly NuGet release v20.3.0.60 which is available for download (https://www.nuget.org/). We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you would require any further assistance.



Loader.
Up arrow icon