Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Hide DevExpress AgDataGrid Header MenuItem in Silverlight

0.00/5 (No votes)
25 Dec 2011CPOL 20.9K  
AgDataGridHeaderMenu

Introduction


DevExpress AgDatagrid has some good features like grouping column, filter column, columnchooser, etc.

Sometimes, we need to hide some of the menu items of AgDataGrid header.

Let's start how to hide AgDataGrid coloumn header menu item.

Step 1: Drag and Drop DataGrid into XAML page from DevExpress ToolBox.

<Grid x:Name="LayoutRoot">
         <dxg:AgDataGrid    FocusMode="Row">
  </dxg:AgDataGrid>
</Grid>


Step 2: Add Column into grid and create event for HeaderMenuShowing

<dxg:AgDataGrid.Columns>
     <dxg:AgDataGridColumn  AllowEditing="False"
         FieldName="SignalTime"
         HeaderContent="Signal Time"
         Width="140"
         HeaderMenuShowing="AgDataGridColumn_HeaderMenuShowing" />
</dxg:AgDataGridColumn>


You have to create event handler for each datagrid column for which you don't want menu items.

Step 3: In code behind (C#), write code to hide unwanted header menu items.

private void AgDataGridColumn_HeaderMenuShowing(object sender, ShowContextMenuEventArgs e)
        {
            int cnt = 0;
            var v = e.Menu.Items as ItemCollection;
            foreach (var d in v)
            {
                if (d.GetType() == typeof(AgMenuItem))
                {
                    AgMenuItem myItem = d as AgMenuItem;
                    if (myItem.Header.ToString() == "Show Group Box" || 
myItem.Header.ToString() == "Remove This Column" || myItem.Header.ToString() == "Show Column Chooser")
                    {
                        myItem.Visibility = Visibility.Collapsed;
                        if (myItem.Header.ToString() == "Show Column Chooser")
                        {
                            cnt = 1;
                        }
                    }
                }
                if (d.GetType() == typeof(AgMenuSeparator) && cnt == 1)
                {
                    (d as AgMenuSeparator).Visibility = Visibility.Collapsed;
                    cnt = 0;
                }
            }
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)