Grid - Context Menu

Next database reset in 0 hours, 27 minutes, 11 seconds
Ship city Show context menu
       Sum : 64942.69
Page size:
 830 items in 56 pages
10248Vins et alcools Chevalier7/4/1996Tuesday, July 16, 1996France Reims 5110032.38
10249Toms Spezialitäten7/5/1996Wednesday, July 10, 1996Germany Münster 4408711.61
10250Hanari Carnes7/8/1996Friday, July 12, 1996Brazil Rio de Janeiro 05454-87665.83
10251Victuailles en stock7/8/1996Monday, July 15, 1996France Lyon 6900441.34
10252Suprêmes délices7/9/1996Thursday, July 11, 1996Belgium Charleroi B-600051.30
10253Hanari Carnes7/10/1996Tuesday, July 16, 1996Brazil Rio de Janeiro 05454-87658.17
10254Chop-suey Chinese7/11/1996Tuesday, July 23, 1996Switzerland Bern 301222.98
10255Richter Supermarkt7/12/1996Monday, July 15, 1996Switzerland Genève 1204148.33
10256Wellington Importadora7/15/1996Wednesday, July 17, 1996Brazil Resende 08737-36313.97
10257HILARION-Abastos7/16/1996Monday, July 22, 1996Venezuela San Cristóbal 502281.91
10258Ernst Handel7/17/1996Tuesday, July 23, 1996Austria Graz 8010140.51
10259Centro comercial Moctezuma7/18/1996Thursday, July 25, 1996Mexico México D.F. 050223.25
10260Ottilies Käseladen7/19/1996Monday, July 29, 1996Germany Köln 5073955.09
10261Que Delícia7/19/1996Tuesday, July 30, 1996Brazil Rio de Janeiro 02389-6733.05
10262Rattlesnake Canyon Grocery7/22/1996Thursday, July 25, 1996USA Albuquerque 8711048.29
  • NoFilter
  • Contains
  • DoesNotContain
  • StartsWith
  • EndsWith
  • EqualTo
  • NotEqualTo
  • GreaterThan
  • LessThan
  • GreaterThanOrEqualTo
  • LessThanOrEqualTo
  • Between
  • NotBetween
  • IsEmpty
  • NotIsEmpty
  • IsNull
  • NotIsNull
  • Custom
  • Sort Ascending
  • Sort Descending
  • Clear Sorting
  • Group By
  • Ungroup
  • Columns
  • Filter
  • Add
  • Edit
  • Delete
Isolate this demo as a stand-alone application
  • About this demo
  • C#
  • VB

This example demonstrates RadGrid's header context filter menu which is a sub-part or RadGrid's header context menu and can be switched on/off using RadGrid.EnableHeaderContextFilterMenu or GridTableView.EnableHeaderContextFilterMenu properties. In addition, there are two indispensable prerequisites for the new filtering feature to be available:

  • the column filtering has to be switched on through AllowColumnFiltering = true.
  • the header context menu must be enabled through EnableHeaderContextMenu = true.

The new filter menu offers the power for filtering the grid's data on two conditions related with a logical AND operator. Two additional properties have been introduced into the GridColumn type in order to support the second filter condition data:

  • AndCurrentFilterFunction.
  • AndCurrentFilterValue.

When a filter command is triggered from the header context filter menu, RadGrid will fire the RadGrid.HeaderContextMenuFilterCommand, raise the ItemCommand event and send into the event handlers the filter data through the event arguments object.

Additionally, in numerous cases you may want to display a context menu when right-clicking an arbitrary row in RadGrid. Through a command from that context menu you may want to change the state for the grid row (for example edit/delete/add record). Furthermore, you may prefer the operation with asynchronous request instead of postback.

The seamless integration between RadContextMenu, RadAjax and RadGrid makes this task very easy to be accomplished. Our grid control exposes OnRowContextMenu client event which can be handled to select the right-clicked record in the grid to mark it as active for the current action. In order to determine the index of the clicked row on the server, use a hidden field on the page to store its value client side and then operate with it on form submit (in the ItemClick event handler of RadMenu). Inside the handler perform the corresponding task chosen by the user to modify the item state.

To associate the context menu with the grid instance, attach the OnRowContextMenu client event of RadGrid, get reference to the context menu object client-side and invoke its show method (passing the browser's event argument as parameter).

Finally, configure the ajax manager settings in order to refresh the grid and the menu in a codeless manner.
  • RadGrid's header context menu can be shown either:
    • on mouse right-click over a header cell
    • or by left-clicking on an element that fires a client-side method of RadGrid's, showing the context menu (see the ShipCity template column)
  • RadGrid's row context menu can be shown by right clicking on each data row.
  • DefaultCS.aspx
  • DefaultCS.aspx.cs
  • scripts.js
<%@ Page Language="c#"  CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.Web.Examples.Integration.GridAndMenu.DefaultCS" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
<html xmlns=''>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <script type="text/javascript" src="scripts.js"></script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdateInitiatorPanelsOnly="true">
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    <telerik:RadGrid RenderMode="Lightweight" AutoGenerateColumns="false" ID="RadGrid1" DataSourceID="SqlDataSource1" Width="100%"
        AllowFilteringByColumn="True" AllowSorting="True" PageSize="15" ShowFooter="True"
        AllowPaging="True" runat="server" GridLines="None" EnableLinqExpressions="false"
        EnableHeaderContextMenu="true" EnableHeaderContextFilterMenu="true" OnPreRender="RadGrid1_PreRender">
        <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
        <GroupingSettings CaseSensitive="false"></GroupingSettings>
        <MasterTableView AutoGenerateColumns="false" IsFilterItemExpanded="false" EditMode="InPlace" DataKeyNames="OrderID"
            AllowFilteringByColumn="True" ShowFooter="True" TableLayout="Auto" AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true">
                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" Visible="false"></telerik:GridEditCommandColumn>
                <telerik:GridNumericColumn DataField="OrderID" HeaderText="OrderID" SortExpression="OrderID"
                    UniqueName="OrderID" ReadOnly="true">
                <telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" SortExpression="ShipName"
                <telerik:GridDateTimeColumn DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"
                    UniqueName="OrderDate" PickerType="None" DataFormatString="{0:d}">
                <telerik:GridDateTimeColumn DataField="ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate"
                    UniqueName="ShippedDate" PickerType="DatePicker" DataFormatString="{0:D}">
                    <HeaderStyle Width="160px"></HeaderStyle>
                <telerik:GridBoundColumn DataField="ShipCountry" HeaderText="ShipCountry" SortExpression="ShipCountry"
                <telerik:GridTemplateColumn DataField="ShipCity" GroupByExpression="ShipCity Group by ShipCity"
                    UniqueName="ShipCity" InitializeTemplatesFirst="false" HeaderStyle-Width="100"
                    HeaderText="Ship city">
                                    <asp:LinkButton ID="lnkSort" runat="server" CommandArgument="ShipCity" CommandName="Sort"
                                        Text="Ship city"></asp:LinkButton>
                                    <img src="Img/Menu.png" style="margin-top: 5px; margin-left: 5px; cursor: pointer"
                                        onclick='demo.ShowColumnHeaderMenu(event,"ShipCity")' alt="Show context menu" />
                        <asp:Label ID="lblCity" runat="server" Text='<%#Eval("ShipCity") %>'></asp:Label>
                <telerik:GridMaskedColumn DataField="ShipPostalCode" HeaderText="ShipPostalCode"
                    SortExpression="ShipPostalCode" UniqueName="ShipPostalCode" Mask="#####">
                    <FooterStyle Font-Bold="true"></FooterStyle>
                <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal" HeaderText="Freight"
                    SortExpression="Freight" UniqueName="Freight" Aggregate="Sum">
                    <FooterStyle Font-Bold="true"></FooterStyle>
            <ClientEvents OnRowContextMenu="demo.RowContextMenu" />
            <Selecting AllowRowSelect="true" />
            <Scrolling AllowScroll="false"></Scrolling>
    <input type="hidden" id="radGridClickedRowIndex" name="radGridClickedRowIndex" />
    <telerik:RadContextMenu ID="RadMenu1" runat="server" OnItemClick="RadMenu1_ItemClick"
        EnableRoundedCorners="true" EnableShadows="true">
            <telerik:RadMenuItem Text="Add">
            <telerik:RadMenuItem Text="Edit">
            <telerik:RadMenuItem Text="Delete">
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Orders]" runat="server" DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID" InsertCommand="INSERT INTO [Orders] ([CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia], [Freight], [ShipName], [ShipAddress], [ShipCity], [ShipRegion], [ShipPostalCode], [ShipCountry]) VALUES (@CustomerID, @EmployeeID, @OrderDate, @RequiredDate, @ShippedDate, @ShipVia, @Freight, @ShipName, @ShipAddress, @ShipCity, @ShipRegion, @ShipPostalCode, @ShipCountry)" UpdateCommand="UPDATE [Orders] SET [CustomerID] = @CustomerID, [EmployeeID] = @EmployeeID, [OrderDate] = @OrderDate, [RequiredDate] = @RequiredDate, [ShippedDate] = @ShippedDate, [ShipVia] = @ShipVia, [Freight] = @Freight, [ShipName] = @ShipName, [ShipAddress] = @ShipAddress, [ShipCity] = @ShipCity, [ShipRegion] = @ShipRegion, [ShipPostalCode] = @ShipPostalCode, [ShipCountry] = @ShipCountry WHERE [OrderID] = @OrderID">
            <asp:Parameter Name="OrderID" Type="Int32" />
            <asp:Parameter Name="CustomerID" Type="String" />
            <asp:Parameter Name="EmployeeID" Type="Int32" />
            <asp:Parameter Name="OrderDate" Type="DateTime" />
            <asp:Parameter Name="RequiredDate" Type="DateTime" />
            <asp:Parameter Name="ShippedDate" Type="DateTime" />
            <asp:Parameter Name="ShipVia" Type="Int32" />
            <asp:Parameter Name="Freight" Type="Decimal" />
            <asp:Parameter Name="ShipName" Type="String" />
            <asp:Parameter Name="ShipAddress" Type="String" />
            <asp:Parameter Name="ShipCity" Type="String" />
            <asp:Parameter Name="ShipRegion" Type="String" />
            <asp:Parameter Name="ShipPostalCode" Type="String" />
            <asp:Parameter Name="ShipCountry" Type="String" />
            <asp:Parameter Name="CustomerID" Type="String" />
            <asp:Parameter Name="EmployeeID" Type="Int32" />
            <asp:Parameter Name="OrderDate" Type="DateTime" />
            <asp:Parameter Name="RequiredDate" Type="DateTime" />
            <asp:Parameter Name="ShippedDate" Type="DateTime" />
            <asp:Parameter Name="ShipVia" Type="Int32" />
            <asp:Parameter Name="Freight" Type="Decimal" />
            <asp:Parameter Name="ShipName" Type="String" />
            <asp:Parameter Name="ShipAddress" Type="String" />
            <asp:Parameter Name="ShipCity" Type="String" />
            <asp:Parameter Name="ShipRegion" Type="String" />
            <asp:Parameter Name="ShipPostalCode" Type="String" />
            <asp:Parameter Name="ShipCountry" Type="String" />
            <asp:Parameter Name="OrderID" Type="Int32" />

Find Assistance

Help Us Improve

Was this example helpful?