【发布时间】:2015-01-14 14:03:38
【问题描述】:
我有一个模型,里面有一个模型列表(简化):
public class GridItemModel {
public int ID { get; set; }
public string Name { get; set; }
public List<GridItemInventoryModel> Inventory { get; set; }
}
public class GridItemInventoryModel {
public int Quantity { get; set; }
}
我像这样将我的数据绑定到网格视图(gridItems 是 GridItemModel 的列表):
gridItems = dal.GetInventoryGrid().ToList();
gridInventory.DataSource = gridItems;
gridInventory 是 DevXpress GridControl。
我已经从 gridview 实现了这些方法:
private void gridView1_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e) {
GridItemModel i = (GridItemModel)gridView1.GetRow(e.RowHandle);
e.ChildList = new BindingSource(i, "Inventory");
}
private void gridView1_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e) {
e.RelationName = "Inventory";
}
private void gridView1_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e) {
e.RelationCount = 1;
}
private void gridView1_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e) {
GridItemModel i = (GridItemModel)gridView1.GetRow(e.RowHandle);
e.IsEmpty = i == null;
}
这让我的网格看起来像这样:
当使用各种过滤器时,子视图中的某些项目被隐藏,我需要最后一列的 Quantity 来反映仅可见子记录的总和。
到目前为止,我已经覆盖了gridView1_CustomUnboundColumnData,如下所述:
http://www.wenda.io/questions/5156824/how-to-make-the-sum-of-each-detail-gridview-and-populated-in-the-footer-of-maste.html
但我没有 DataRows,它们只能转换为 GridItemModel。
我可以访问网格中的项目:
GridView view = sender as GridView;
if (e.Column.FieldName != "colCalcQty") return;
if (!e.IsGetData) return;
var list = (view.DataSource as IList);
var item = (list[e.ListSourceRowIndex] as GridItemModel);
但 item.Inventory 始终显示所有库存模型,而不是过滤器未删除的模型。
我可以分别通过 gridView1 和 gridView2 访问父网格和子网格。
如何使用我的设置从可见的子视图数量字段创建一个汇总列?
编辑: 我用几种不同的方式过滤我的视图,首先我在我的网格上启用了 ShowAutoFilterRow 但这只会过滤父记录。
然后我在表单的其他地方有一个组合框,在选定的索引更改事件上调用此代码:
InventoryLibrary.DataTransferObjects.SimpleLocationModel location = ((ComboBoxEdit)cbeLocationsFilter).SelectedItem as InventoryLibrary.DataTransferObjects.SimpleLocationModel;
InventoryLibrary.DataTransferObjects.SimpleLocationModel subLocation = ((ComboBoxEdit)cbeSubLocationsFilter).SelectedItem as InventoryLibrary.DataTransferObjects.SimpleLocationModel;
string itemFilterString = "";
string locationFilterString = "";
if (location != null) {
itemFilterString += String.Format("[LocationNames] LIKE '%{0}%'", location.Name);
locationFilterString += String.Format("[LocationAndSublocation] LIKE '%{0}%'", location.Name);
}
if (subLocation != null) {
if (location != null) itemFilterString += " AND ";
itemFilterString += String.Format("[LocationNames] LIKE '%{0}%'", subLocation.Name);
if (location != null) locationFilterString += " AND ";
locationFilterString += String.Format("[LocationAndSublocation] LIKE '%{0}%'", subLocation.Name);
}
gridView1.ActiveFilterString = itemFilterString;
gridView2.ActiveFilterString = locationFilterString;
当gridView2.ActiveFilterString 应用了过滤字符串时,这会过滤详细视图。
【问题讨论】:
标签: c# gridview devexpress