Wie kann ich in einem Listview einen Counter einfügen der mir die Anzahl der markierten Elemente im ListView zurück gibt?
Dazu habe ich einen View Controller erstellt. der mir folgende Optionen zur Verfügung stellt:
1. ShowFooter wird in allen Listviews auf enabled gesetzt
2. Das SummenMenü in der Fußzeile wird um einen weiteren Eintrag „Anz. Markiert“ erweitert
3. Drei EventHanlder sorgen dafür dass der Count aktualisiert wird.
Hier mein Code:
using System; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Win.Editors; using DevExpress.XtraGrid.Columns; using DevExpress.Data; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid; namespace GIS_V2_Ticket_Edition.Module.Controllers { public partial class ShowSelectionCount_Controller : ViewController { GridListEditor _listEditor; public ShowSelectionCount_Controller() { InitializeComponent(); } protected override void OnActivated() { base.OnActivated(); } protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); _listEditor = ((ListView)View).Editor as GridListEditor; if (_listEditor == null) return; _listEditor.GridView.OptionsView.ShowFooter = true; _listEditor.GridView.SelectionChanged += new SelectionChangedEventHandler(gridView1_SelectionChanged); _listEditor.GridView.PopupMenuShowing += new PopupMenuShowingEventHandler(gridView1_PopupMenuShowing); _listEditor.GridView.CustomSummaryCalculate += new CustomSummaryEventHandler(CustomSummaryCalculate); } protected override void OnDeactivated() { // Unsubscribe from previously subscribed events and release other references and resources. base.OnDeactivated(); } protected void gridView1_SelectionChanged(object sender, SelectionChangedEventArgs e) { GridView view = sender as GridView; view.UpdateTotalSummary(); } protected void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { if (e.MenuType != DevExpress.XtraGrid.Views.Grid.GridMenuType.Summary) return; DevExpress.XtraGrid.Menu.GridViewFooterMenu footerMenu = e.Menu as DevExpress.XtraGrid.Menu.GridViewFooterMenu; bool check = e.HitInfo.Column.SummaryItem.SummaryType == DevExpress.Data.SummaryItemType.Custom && Equals("Anz. Markierte", e.HitInfo.Column.SummaryItem.Tag); DevExpress.Utils.Menu.DXMenuItem menuItem = new DevExpress.Utils.Menu.DXMenuCheckItem("Anz. Markierte", check, null, new EventHandler(MyMenuItem)); menuItem.Tag = e.HitInfo.Column; foreach (DevExpress.Utils.Menu.DXMenuItem item in footerMenu.Items) item.Enabled = true; footerMenu.Items.Add(menuItem); } private void MyMenuItem(Object sender, EventArgs e) { DevExpress.Utils.Menu.DXMenuItem Item = sender as DevExpress.Utils.Menu.DXMenuItem; GridColumn col = Item.Tag as GridColumn; col.SummaryItem.Tag = "Anz. Markierte"; col.SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Custom, string.Empty); } protected void CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e) { GridColumnSummaryItem item = e.Item as GridColumnSummaryItem; GridView view = sender as GridView; if (Equals("Anz. Markierte", item.Tag)) { e.TotalValue = view.SelectedRowsCount; } } } }