GridWeb でコンテキスト メニュー項目を追加または削除する

ASP.NET マークアップを使用してコンテキスト メニュー項目を追加する

次の ASP.NET マークアップは、GridWeb にコンテキスト メニュー項目を追加します。

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
<CustomCommandButtons>
<acw:CustomCommandButton Command="MyContextMenuItemCommand" Text="ContextMenuItemText" CommandType="ContextMenuItem"></acw:CustomCommandButton>
</CustomCommandButtons>

上記のコンテキスト メニュー項目で GridWeb を作成する完全な ASP.NET マークアップを次に示します。 OnCustomCommand=“GridWeb1_CustomCommand” 属性に注意してください。コンテキスト メニュー項目がクリックされたときに呼び出されるイベント ハンドラ名です。

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
<acw:GridWeb ID="GridWeb1" runat="server" XhtmlMode="True" EnableAJAX="true" OnCustomCommand="GridWeb1_CustomCommand" EnableClientColumnOperations="False" EnableClientFreeze="False" EnableClientMergeOperations="False" EnableClientRowOperations="False" EnableStyleDialogbox="False">
<CustomCommandButtons>
<acw:CustomCommandButton Command="MyContextMenuItemCommand" Text="ContextMenuItemText" CommandType="ContextMenuItem"></acw:CustomCommandButton>
</CustomCommandButtons>
</acw:GridWeb>

上記の ASP.NET マークアップを使用して追加した後のコンテキスト メニュー項目は次のようになります。

todo:画像_代替_文章

これは、コンテキスト メニュー項目がクリックされたときに実行されるイベント ハンドラー コードです。コードは最初にコマンド名をチェックし、コマンドと一致する場合は、アクティブな GridWeb ワークシートのセル A1 にテキストを追加し、最初の列幅を 40 単位に設定してテキストを表示します。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Event Handler for custom command event of GridWeb
protected void GridWeb1_CustomCommand(object sender, string command)
{
if (command.Equals("MyContextMenuItemCommand"))
{
// Accessing the active sheet
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Putting value to "A1" cell
sheet.Cells["A1"].PutValue("My Custom Context Menu Item is Clicked.");
// Set first column width to make the text visible
sheet.Cells.SetColumnWidth(0, 40);
}
}

これは、コンテキスト メニュー項目をクリックしたときの GridWeb の外観です。

todo:画像_代替_文章

コードを使用して Aspose.Cells.GridWeb にコンテキスト メニュー項目を追加する

このコードは、コードを使用して GridWeb 内にコンテキスト メニュー項目を追加する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Init context menu item command button
CustomCommandButton cmd = new CustomCommandButton();
cmd.CommandType = CustomCommandButtonType.ContextMenuItem;
cmd.Text = "MyNewContextMenuItem";
cmd.Command = "MyNewContextMenuItemCommand";
// Add context menu item command button to GridWeb
GridWeb1.CustomCommandButtons.Add(cmd);

コードを使用して Aspose.Cells.GridWeb のコンテキスト メニュー項目を削除する

このコードは、CustomCommandButtons.Remove() および CustomCommandButtons.RemoveAt() メソッドを使用してコンテキスト メニュー項目を削除する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
if (GridWeb1.CustomCommandButtons.Count > 1)
{
// Remove the 2nd custom command button or context menu item using remove at method
GridWeb1.CustomCommandButtons.RemoveAt(1);
}
if (GridWeb1.CustomCommandButtons.Count >= 1)
{
// Access the 1st custom command button or context menu item and remove it
CustomCommandButton custbtn = GridWeb1.CustomCommandButtons[0];
GridWeb1.CustomCommandButtons.Remove(custbtn);
}