Home Forums WPF controls Xceed DataGrid for WPF Select columns to be displayed in a Grid

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26769 |

     

    Ability for user to select columns for the grid from a larger set of possible columns. Is there any inbuilt option like to select the columns to be shown and hide other columns.

    Imported from legacy forums. Posted by Rachana (had 536 views)

    Xceed Support
    Member
    Post count: 5658

    There is no built-in UI that allows the end-user of the DataGrid for WPF to select what columns to display. You would need to build this yourself.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 274 views)

    User (Old forums)
    Member
    Post count: 23064

    You could use the good old (right click) context menu. Here is a start; just fill in the blanks.

                grid.ContextMenu = new ContextMenu();
                grid.ContextMenuOpening += new ContextMenuEventHandler(temps.cm_ContextMenuOpening);

    ///////////////////////////////////////////////////////////////////

     

            public void cm_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                ContextMenu cm = new ContextMenu();
                DataGridControl grid = sender as DataGridControl;
                if (grid == null) return;
                Column kc = grid.CurrentColumn as Column;
                if (kc == null) return;
                if (cm != null)
                {
                    MenuItem l = new MenuItem();
                    MenuItem mi = new MenuItem();
                    mi.Header = “Save As Defaults”;
                    mi.Tag = grid;
                    mi.Click += new RoutedEventHandler(mi_Click);

                    cm.Items.Add(mi);
                    TextBox tb = new TextBox();
                    if (grid.CurrentColumn != null)
                    {
                        Binding b = new Binding(“Title”);
                        b.Source = grid.CurrentColumn;

                        tb.SetBinding(TextBox.TextProperty, b);
                        cm.Items.Add(tb);
                    }

                    //Add visible
                    mi = new MenuItem();
                    mi.Header = “Read Only”;
                    mi.IsCheckable = true;
                    mi.IsChecked = kc.ReadOnly;
                    mi.Tag = grid;
                    mi.Click += new RoutedEventHandler(mi_ReadOnly_Click);
                    cm.Items.Add(mi);

                    //visible column selection
                    mi = new MenuItem();
                    mi.Header = “Show Columns”;
                    int j = -1;
                    while (++j < grid.Columns.Count)
                    {
                        Column c = grid.Columns[j];
                        MenuItem smi = new MenuItem();
                        smi.Header = c.FieldName;
                        Binding vb = new Binding(“Visible”);
                        vb.Source = c;
                        smi.SetBinding(MenuItem.IsCheckedProperty, vb);
                        smi.Click += new RoutedEventHandler(smi_Visible_Click);
                        smi.Tag = c;
                        mi.Items.Add(smi);
                    }
                    cm.Items.Add(mi);

                    mi = new MenuItem();
                    mi.Header = kc.VisiblePosition;
                    cm.Items.Add(mi);

                    mi = new MenuItem();
                    mi.Header = this.getCellTemplateName(kc.CellContentTemplate);
                    cm.Items.Add(mi);

                    mi = new MenuItem();
                    mi.Header = this.getCellEditorName(kc.CellEditor);
                    cm.Items.Add(mi);

                    grid.ContextMenu = cm;
                }

            }

     

    ////////////////////////////////////////////////////////////////

     

    But I bet you want to be able to save all those settings you just gave the user.

    I loop through the grid and persist all the columns settings(Readonly, Title, width, visible position, etc..) back to a SQL server (you could use an XML file too)

    Then load those at startup.

    I would post this code but it is a bit long and very personalized that it would confuse a newbie and a non newbie would be able to write new code from scratch faster than depersonalizing my code.  

     

    But if the community wants it just as an example to build their own I would be glad to post Bits of it(w/o comments of course). 

     

    Imported from legacy forums. Posted by MiddleTommy (had 1764 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.