现在的位置: 首页 > 综合 > 正文

WPF DataGrid中嵌入Combobox

2013年10月03日 ⁄ 综合 ⁄ 共 26939字 ⁄ 字号 评论关闭

页面

<dsgc:DataSetListEntityControl x:Class="BMITQT.Quotation.QuotationComponentList"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:igDP="http://infragistics.com/DataPresenter"

    xmlns:sys="clr-namespace:System;assembly=mscorlib"

    xmlns:mgw="clr-namespace:Mg2.Windows;assembly=Mg2.Windows"

    xmlns:dsgc="clr-namespace:BMITQT.BuziLogic;assembly=BMITQT.BuziLogic"

    xmlns:mgc="clr-namespace:Mg2.Windows.Controls;assembly=Mg2.Windows"

    xmlns:mig="clr-namespace:Mg2.Ig.Windows;assembly=Mg2.Ig.Windows"

    xmlns:igEditors="http://infragistics.com/Editors"

    >

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="20" ></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="30"></ColumnDefinition>

            <ColumnDefinition Width="*"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0"  Grid.Column="0" Height="20" Background="White"></TextBlock>

        <TextBlock Name="txtCaption"  Grid.Column="1" Text=" Quotation Component" Grid.Row="0" Background="White"></TextBlock>

        

        <mig:MigDataGrid Grid.Row="1" Grid.ColumnSpan="2"  x:Name="grdQuotationComponent" Theme="Office2k7Black"  MouseDoubleClick="grdQuotationComponent_MouseDoubleClick" InitializeRecord="grdQuotationComponent_InitializeRecord"  CellUpdated="grdQuotationComponent_CellUpdated" CellActivating="grdQuotationComponent_CellActivating"  GroupByAreaLocation="None">

            <igDP:XamDataGrid.Resources>

                <!-- This items provider is populated in the code-behind. -->

                <igEditors:ComboBoxItemsProvider x:Key="StatusItemsProvider" />

                <Style x:Key="StatusFieldStyle" TargetType="{x:Type igEditors:XamComboEditor}">

                    <Setter Property="ItemsProvider" Value="{StaticResource StatusItemsProvider}" />

                </Style>

            </igDP:XamDataGrid.Resources>

            <igDP:XamDataGrid.FieldLayouts>

                <igDP:FieldLayout >

                    <igDP:FieldLayout.Fields>

                        <igDP:Field  Name="QuotationComponentId" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblQuotationComponentId}" Visibility="Collapsed">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="0"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="ComponentCode" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblComponentCode}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="ComponentTypeName" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblComponentTypeName}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="StatisticsTypeName" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblStatisticsTypeName}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="ComponentUnitName" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblUnit}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="ManufacturerName" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblManufacturer}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="Specification" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblSpecification}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="Price"  Label="{StaticResource BMITQT.Module.ModuleComponent_ComponentCostList.Price}">                            

                            <igDP:Field.Settings>

                                <igDP:FieldSettings EditorStyle="{StaticResource StatusFieldStyle}" CellWidth="100" LabelWidth="100" CellValuePresenterStyle="{StaticResource EditCellStyle}" />

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="Quantity" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblQuantity}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="True" EditAsType="{x:Type sys:Int32}"  CellWidth="100" CellClickAction="EnterEditModeIfAllowed" CellValuePresenterStyle="{StaticResource EditCellStyle}" />

                            </igDP:Field.Settings>

                        </igDP:Field>

                        <igDP:Field  Name="Description" Label="{StaticResource BMITQT.Quotation.QuotationComponentList.lblDescription}">

                            <igDP:Field.Settings>

                                <igDP:FieldSettings AllowEdit="False" CellWidth="50" CellClickAction="SelectRecord"/>

                            </igDP:Field.Settings>

                        </igDP:Field>

                    </igDP:FieldLayout.Fields>

                </igDP:FieldLayout>

            </igDP:XamDataGrid.FieldLayouts>

 

            <igDP:XamDataGrid.ContextMenu>

                <ContextMenu Width="150" Background="WhiteSmoke"  Margin="15">

 

                    <MenuItem Name="menuAdd" Margin="3"  Click="menuAdd_Click" Header="{StaticResource UI.Common.ListWindow.Menu.Add}">

                        <MenuItem.Icon>

                            <Image Source="/Icons/PopUpAdd.GIF"></Image>

                        </MenuItem.Icon>

                    </MenuItem>

 

                    <MenuItem Name="menuModify" Margin="3" Click="menuModify_Click" Header="{StaticResource UI.Common.ListWindow.Menu.Modify}">

                        <MenuItem.Icon>

                            <Image Source="/Icons/PopUpModify.GIF"></Image>

                        </MenuItem.Icon>

                    </MenuItem>

 

                    <MenuItem Name="menuDelete" Margin="3" Click="menuDelete_Click" Header="{StaticResource UI.Common.ListWindow.Menu.Delete}">

                        <MenuItem.Icon>

                            <Image Source="/Icons/PopUpDelete.GIF"></Image>

                        </MenuItem.Icon>

                    </MenuItem>

                    <MenuItem Name="menuComponentCost" Margin="3" Click="menuComponentCost_Click" Header="{StaticResource UI.Common.QuotationComponentList.Menu.QuotationComponentCost}">

                        <MenuItem.Icon>

                            <Image Source="/Icons/PopUpAdd.GIF"></Image>

                        </MenuItem.Icon>

                    </MenuItem>

                    

                    <MenuItem Name="menuComponentPrice" Margin="3" Click="menuComponentPrice_Click" Header="{StaticResource UI.Common.QuotationComponentList.Menu.QuotationComponentPrice}">

                        <MenuItem.Icon>

                            <Image Source="/Icons/PopUpAdd.GIF"></Image>

                        </MenuItem.Icon>

                    </MenuItem>

 

                </ContextMenu>

            </igDP:XamDataGrid.ContextMenu>

        </mig:MigDataGrid>

    </Grid>

</dsgc:DataSetListEntityControl>

后台代码
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using BMITQT.BuziLogic.Common;
using BMITQT.BuziLogic;
using BMITQT.Po;
using BMITQT.Common;
using Mg2.Data;
using Mg2.Windows.Common;
using Mg2.Windows.Controls;
using Mg2.Core;
using Infragistics.Windows.DataPresenter;
using Infragistics.Windows.Editors;
namespace BMITQT.Quotation
{
    /// <summary>
    /// QuotationComponentList.xaml 的交互逻辑
    /// </summary>
    public partial class QuotationComponentList : DataSetListEntityControl
    {
        private new string IdentifierName = "QuotationComponentId"; //Rewrite IdentifierName
        private DataTable _categoryCostSummaryDt = new DataTable(); //Store parent quoation category summary cost 
        public long? QuotationCategoryId { get; set; }
        public QuotationComponentList()
        {
            InitializeComponent();
            _entityType = typeof(BMITQT.Po.QuotationComponent);
            _dataSource = new DataTable();
        }
        //Load Grid Data
        public void LoadGridData()
        {
            AddSummaryFields();
            DataSet ds = new DataSet();
            DataTable dtQuotationComponent = GetQuotationComponent(); //Get parent datatable
            DataTable dtCost = GetQuotationComponentCost(dtQuotationComponent); //Get child datatable
            ds.Tables.Add(dtQuotationComponent);
            ds.Tables.Add(dtCost);
            //Create relation between parent datatable and child datatable
            ds.Relations.Add(new DataRelation("QuotationComponentQutationComponentCost", dtQuotationComponent.Columns["QuotationComponentId"], ds.Tables[1].Columns["QuotationComponentId"]));
            this.grdQuotationComponent.DataSource = dtQuotationComponent.DefaultView;
        }
        //Get Quotation Component DataTable (Parent DataTable)
        protected DataTable GetQuotationComponent()
        {
            string rowFilerString = "";
            DataTable dtQuotationComponent = new DataTable();
            dtQuotationComponent.Columns.Add("QuotationComponentId", typeof(Int64));
            dtQuotationComponent.Columns.Add("ComponentCode", typeof(string));
            dtQuotationComponent.Columns.Add("Description", typeof(string));
            dtQuotationComponent.Columns.Add("Price", typeof(float));
            dtQuotationComponent.Columns.Add("Quantity", typeof(float));
            dtQuotationComponent.Columns.Add("Specification", typeof(string));
            dtQuotationComponent.Columns.Add("ManufacturerName");
            dtQuotationComponent.Columns.Add("ComponentTypeName");
            dtQuotationComponent.Columns.Add("ComponentUnitName");
            dtQuotationComponent.Columns.Add("StatisticsTypeName");
            dtQuotationComponent.Columns.Add("Material Cost SubTotal");
            dtQuotationComponent.Columns.Add("Subtotal");
            
            if (QuotationCategoryId == null)
                rowFilerString = "QuotationCategoryId is null";
            else
                rowFilerString = "QuotationCategoryId = " + QuotationCategoryId.Value;
            this.QuotationDataSet.QuotationComponent.DefaultView.RowFilter = rowFilerString;
            this.QuotationDataSet.QuotationComponent.DefaultView.Sort = "ComponentCode";
            foreach (DataRowView quotationComponentDr in this.QuotationDataSet.QuotationComponent.DefaultView)
            {
                DataRow newRow = dtQuotationComponent.NewRow();
                newRow["QuotationComponentId"] = quotationComponentDr["QuotationComponentId"];
                newRow["ComponentCode"] = quotationComponentDr["ComponentCode"];
                newRow["Description"] = quotationComponentDr["Description"];
                newRow["Quantity"] = quotationComponentDr["Quantity"];
                newRow["Specification"] = quotationComponentDr["Specification"];
                newRow["ManufacturerName"] = this.QuotationDataSet.Manufacturer.FindByManufacturerId((long)quotationComponentDr["ManufacturerId"]).ManufacturerName;
                if (quotationComponentDr["ComponentTypeId"] != DBNull.Value)
                    newRow["ComponentTypeName"] = this.QuotationDataSet.ComponentType.FindByComponentTypeId((long)quotationComponentDr["ComponentTypeId"]).ComponentTypeName;
                else
                    newRow["ComponentTypeName"] = "";
                if (quotationComponentDr["ComponentUnitId"] != DBNull.Value)
                    newRow["ComponentUnitName"] = this.QuotationDataSet.ComponentUnit.FindByComponentUnitId((long)quotationComponentDr["ComponentUnitId"]).ComponentUnitName;
                else
                    newRow["ComponentUnitName"] = "";
                newRow["StatisticsTypeName"] = this.QuotationDataSet.StatisticsType.FindByStatisticsTypeId((long)quotationComponentDr["StatisticsTypeId"]).StatisticsTypeName;
                if (quotationComponentDr["QuotationComponentPriceId"] != DBNull.Value)
                    newRow["Price"] = this.QuotationDataSet.QuotationComponentPrice.FindByQuotationComponentPriceId((long)quotationComponentDr["QuotationComponentPriceId"]).Price;
                //else
                //    newRow["Price"] = "";
                long quotationComponentId = long.Parse(quotationComponentDr["QuotationComponentId"].ToString());
                newRow["Material Cost SubTotal"] = DataSetCalculateUtil.GetQuotationComponentMaterialCostSubtotal(quotationComponentId, this.QuotationController);
                newRow["SubTotal"] = DataSetCalculateUtil.GetQuotationComponentSubtotal(quotationComponentId, this.QuotationController);
                dtQuotationComponent.Rows.Add(newRow);
            }
            return dtQuotationComponent;
        }
        //Get Quotation Component Cost DataTable (Child DataTable)
        protected DataTable GetQuotationComponentCost(DataTable dtQuotationComponent)
        {
            List<string> quotationComponentIds = new List<string>();
            foreach (DataRow dr in dtQuotationComponent.Rows)
            {
                quotationComponentIds.Add(dr["QuotationComponentId"].ToString());
            }
            string[] Ids = quotationComponentIds.ToArray();
            DataTable dtCost = new DataTable();
            dtCost.Columns.Add("QuotationComponentCostId", typeof(Int64));
            dtCost.Columns.Add("QuotationComponentId", typeof(Int64));
            dtCost.Columns.Add("Unit", typeof(string));
            dtCost.Columns.Add("Currency", typeof(string));
            dtCost.Columns.Add("Calculate Method", typeof(string));
            dtCost.Columns.Add("Quantity", typeof(float));
            dtCost.Columns.Add("Rate", typeof(float));
            dtCost.Columns.Add("Amount", typeof(float));
            dtCost.Columns.Add("Unit SubTotal", typeof(string));
            dtCost.Columns.Add("SubTotal", typeof(string));
            
            //If there are parent quotation component, get quotation component cost in these quotation components
            if (Ids.Length > 0)
            {
                string rowFilterString = "QuotationComponentId in (" + string.Join(",", Ids) + ")";
                QuotationDataSet.QuotationComponentCost.DefaultView.RowFilter = rowFilterString;
                foreach (DataRowView dr in QuotationDataSet.QuotationComponentCost.DefaultView)
                {
                    DataRow newRow = dtCost.NewRow();
                    newRow["QuotationComponentCostId"] = dr["QuotationComponentCostId"];
                    newRow["QuotationComponentId"] = dr["QuotationComponentId"];
                    if (dr["CostUnitId"] != DBNull.Value)
                        newRow["Unit"] = this.QuotationDataSet.CostUnit.FindByCostUnitId((long)dr["CostUnitId"]).CostUnitName;
                    else
                        newRow["Unit"] = "";
                    if (dr["CurrencyId"] != DBNull.Value)
                        newRow["Currency"] = this.QuotationDataSet.Currency.FindByCurrencyId((long)dr["CurrencyId"]).CurrencyCode;
                    else
                        newRow["Currency"] = "";
                    if (dr["CostCalculateMethodId"] != DBNull.Value)
                        newRow["Calculate Method"] = this.QuotationDataSet.CostCalculateMethod.FindByCostCalculateMethodId((long)dr["CostCalculateMethodId"]).CostCalculateMethodName;
                    else
                        newRow["Calculate Method"] = "";
                    newRow["Quantity"] = dr["Quantity"];
                    newRow["Rate"] = dr["Rate"];
                    newRow["Amount"] = dr["Amount"];
                    long quotationComponentCostId = long.Parse(dr["QuotationComponentCostId"].ToString());
                    newRow["Unit SubTotal"] = DataSetCalculateUtil.GetQuotationComponentCostUnitSubtotal(quotationComponentCostId, this.QuotationController);
                    newRow["SubTotal"] = DataSetCalculateUtil.GetQuotationComponentCostSubtotal(quotationComponentCostId, this.QuotationController);
                    dtCost.Rows.Add(newRow);
                }
            }
            return dtCost;
        }
        protected override void AssignDataSourceRowValue(DataRow dataSourceRow, long entityRecordId)
        {
        }
        #region implement Mg2ListEntityControl
        protected override IDataGrid DataGrid
        {
            get { return grdQuotationComponent; }
        }
        protected override DataSetEditEntityWindow CreateEditWindow()
        {
            QuotationComponentDef quotationComponentDef = new QuotationComponentDef();
            quotationComponentDef.QuotationCategoryId = this.QuotationCategoryId;
            quotationComponentDef.QuotationController = this.QuotationController;
            return quotationComponentDef;
        }
        #endregion
        #region Rewrite Modify, Delete, Add Funciton
        //Handle the logic when modify the record 
        private new void OnOpenEntityRecord()
        {
            if (DataGrid.ActiveDataItem == null)
            {
                string _message = ResourceUtil.GetString("UI.Common.ListWindow.SelectOneRow");
                MessageBox.Show(_message);
            }
            else
            {
                DataSetEditEntityWindow editWindow = CreateEditWindow();
                if (typeof(DataRowView).IsInstanceOfType(DataGrid.ActiveDataItem))
                {
                    DataRow activeDataRow = ((DataRowView)DataGrid.ActiveDataItem).Row;
                    long entityRecordId = (long)activeDataRow[IdentifierName];
                    editWindow.EntityRecordId = entityRecordId;
                    editWindow.ShowDialog();
                    if (editWindow.IsSaved)
                    {
                        LoadGridData();                        
                        //this.RaiseEntityModifyEvent(editWindow.EntityRecord);
                    }
                }
                else
                {
                    string _message = ResourceUtil.GetString("Entity.Common.DataSource.NotDataTable");
                    throw new Mg2Exception(_message, null, Mg2Exception.SystemErrorMinID);
                }
            }
        }
        //Handle the process when delete record
        private new void OnDeleteEntityRecord()
        {
            if (DataGrid.ActiveDataItem == null)
            {
                string _message = ResourceUtil.GetString("UI.Common.ListWindow.SelectOneRow");
                MessageBox.Show(_message);
            }
            else
            {
                string _confirmText = ResourceUtil.GetString("UI.Common.ListWindow.DeleteConfirmBox.Text");
                string _confirmCaption = ResourceUtil.GetString("UI.Common.ListWindow.DeleteConfirmBox.Caption");
                if (MessageBox.Show(_confirmText, _confirmCaption, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    DataRow activeDataRow = GetActiveDataRow();
                    long entityRecordId = (long)activeDataRow[IdentifierName];
                    object entityRecord = this.QuotationController.GetEntity(_entityType, entityRecordId);
                    try
                    {
                        DataSetEntityDeleteUtil.QuotationDeleteFacade(entityRecord, this.QuotationController);
                        LoadGridData();
                    }
                    catch (Exception e)
                    {
                        WindowUtil.ShowException(e, null);
                    }
                }
            }
        }
        //Hanle the process when add entity
        private new void OnAddEntityRecord()
        {
            DataSetEditEntityWindow editWindow = CreateEditWindow();
            editWindow.ShowDialog();
            if (editWindow.IsSaved)
            {
                LoadGridData();
            }
        }
        #endregion
        #region UI Element Event
        private void menuAdd_Click(object sender, RoutedEventArgs e)
        {
            OnAddEntityRecord();
        }
        private void menuModify_Click(object sender, RoutedEventArgs e)
        {
            OnOpenEntityRecord();
        }
        private void menuDelete_Click(object sender, RoutedEventArgs e)
        {
            OnDeleteEntityRecord();
        }
        private void grdQuotationComponent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            OnOpenEntityRecord();
        }
        private void menuComponentCost_Click(object sender, RoutedEventArgs e)
        {
            if (DataGrid.ActiveDataItem == null)
            {
                string _message = ResourceUtil.GetString("UI.Common.ListWindow.SelectOneRow");
                MessageBox.Show(_message);
            }
            else
            {
                DataRow activeDataRow = GetActiveDataRow();
                long entityRecordId = long.Parse(activeDataRow["QuotationComponentId"].ToString());
                QuotationComponentCostList list = new QuotationComponentCostList();
                list.quotationComponentCostListCtr.QuotationController = this.QuotationController;
                list.QuotationComponentId = entityRecordId;
                list.ShowDialog();
            }
            this.LoadGridData();
        }
        private void menuComponentPrice_Click(object sender, RoutedEventArgs e)
        {
            if (DataGrid.ActiveDataItem == null)
            {
                string _message = ResourceUtil.GetString("UI.Common.ListWindow.SelectOneRow");
                MessageBox.Show(_message);
            }
            else
            {
                DataRow activeDataRow = GetActiveDataRow();
                long entityRecordId = long.Parse(activeDataRow["QuotationComponentId"].ToString());
                QuotationComponentPriceList list = new QuotationComponentPriceList();
                list.QuotationController = this.QuotationController;
                list.QuotationComponentId = entityRecordId;
                list.ShowDialog();
            }
            this.LoadGridData();
        }
        private void grdQuotationComponent_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)
        {
            if (e.Record is DataRecord)
            {
                DataRecord record = (DataRecord)e.Record;
                long quotationComponentId = long.Parse(record.Cells["QuotationComponentId"].Value.ToString());
                foreach (DataRow categoryCostSummaryDr in _categoryCostSummaryDt.Rows)
                {
                    string formula = categoryCostSummaryDr["Formula"].ToString();
                    double summaryValue = DataSetCalculateUtil.GetQuotationComponentSummaryValue(quotationComponentId, formula, this.QuotationController);
                    record.Cells[categoryCostSummaryDr["CostTypeName"].ToString()].Value = summaryValue;
                }
            }
        }
        private void grdQuotationComponent_CellActivating(object sender, Infragistics.Windows.DataPresenter.Events.CellActivatingEventArgs e)
        {
            long quotationComponentId = long.Parse(e.Cell.Record.Cells["QuotationComponentId"].Value.ToString());
            DataRow[] drs = GetComponentPrice(quotationComponentId);
            ComboBoxItemsProvider statusProvider = this.grdQuotationComponent.TryFindResource("StatusItemsProvider") as ComboBoxItemsProvider;
            if (statusProvider != null)
            {
                List<ComboBoxDataItem> items = new List<ComboBoxDataItem>();
                foreach (DataRow dr in drs)
                {
                    items.Add(new ComboBoxDataItem(dr["QuotationComponentPriceId"], dr["price"].ToString()));
                }
                statusProvider.ItemsSource = items.ToArray();
            }
        }
        private DataRow[] GetComponentPrice(long quotationComponentId)
        {
            string str = "QuotationComponentId = " + quotationComponentId;
            DataRow[] drs = this.QuotationDataSet.QuotationComponentPrice.Select(str);
            return drs;
        }
        private void grdQuotationComponent_CellUpdated(object sender, Infragistics.Windows.DataPresenter.Events.CellUpdatedEventArgs e)
        {
            if (this.grdQuotationComponent.ActiveDataItem is DataRowView)
            {
                long? quotationComponentId = (long)((DataRowView)this.grdQuotationComponent.ActiveDataItem).Row["QuotationComponentId"];
                double price = double.Parse(e.Cell.Value.ToString());
                CellValuePresenter cvp = CellValuePresenter.FromRecordAndField(e.Record, this.grdQuotationComponent.FieldLayouts[0].Fields["Price"]);
                XamComboEditor xce = cvp.Editor as XamComboEditor;
                long quotationComponentPriceId = long.Parse((((Infragistics.Windows.Editors.ComboBoxDataItem)(xce.SelectedItem)).Value).ToString());
                QuotationComponent quotationComponent = (QuotationComponent)this.QuotationController.GetEntity(typeof(QuotationComponent), quotationComponentId);
                if (e.Field.Name == "Quantity")
                {
                    quotationComponent.Quantity = double.Parse(e.Cell.Value.ToString());
                }
                if (e.Field.Name == "Price")
                {
                    quotationComponent.QuotationComponentPriceId = quotationComponentPriceId;
                }
                this.QuotationController.MemorySaveOrUpdate(quotationComponent);
            }
            LoadGridData();
        }
        #endregion
        #region Handle Summary Fields
        //Add summary fields
        private void AddSummaryFields()
        {
            RemoveSummaryFields();
            this._categoryCostSummaryDt.Clear();
            this._categoryCostSummaryDt.Columns.Clear();
            string[] categoryCostSummaryDtFields = { "CostTypeId", "CostTypeName", "Formula" };
            foreach (string categoryCostSummaryDtField in categoryCostSummaryDtFields)
            {
                _categoryCostSummaryDt.Columns.Add(categoryCostSummaryDtField);
            }
            string categoryCostFilterString = "CostCalculateMethodId = 4 ";
            if (QuotationCategoryId == null)
                categoryCostFilterString += " And QuotationCategoryId is null";
            else
                categoryCostFilterString += " And QuotationCategoryId = " + this.QuotationCategoryId.Value;
            this.QuotationController.QuotationDataSet.QuotationCategoryCost.DefaultView.RowFilter = categoryCostFilterString;
            this.QuotationController.QuotationDataSet.QuotationCategoryCost.DefaultView.Sort = "QuotationCategoryCostId";
            foreach (DataRowView categoryCostDrv in this.QuotationController.QuotationDataSet.QuotationCategoryCost.DefaultView)
            {
                DataRow newRow = _categoryCostSummaryDt.NewRow();
                foreach (string fieldName in categoryCostSummaryDtFields)
                    newRow[fieldName] = categoryCostDrv[fieldName];
                _categoryCostSummaryDt.Rows.Add(newRow);
            }
            foreach (DataRow categoryCostSummaryDr in _categoryCostSummaryDt.Rows)
            {
                AddNewUnboundField(categoryCostSummaryDr["CostTypeName"].ToString(), categoryCostSummaryDr["CostTypeName"].ToString());
                AddSummaryDefinitionField(categoryCostSummaryDr["CostTypeName"].ToString());
            }
            //add dumb field
            //if (this.grdQuotationCategoryCost.FieldLayouts[0].Fields.IndexOf(Constants.conDumbUnboundFieldName) == -1)
            //{
            //    AddNewUnboundField(Constants.conDumbUnboundFieldName, Constants.conDumbUnboundFieldName);
            //    AddSummaryDefinitionField(Constants.conDumbUnboundFieldName);
            //    this.grdList.FieldLayouts[0].Fields[Constants.conDumbUnboundFieldName].Visibility = Visibility.Hidden;
            //}
        }
        //Add unbound field
        private void AddNewUnboundField(string name, string label)
        {
            UnboundField unboundField = new UnboundField();
            unboundField.Name = name;
            unboundField.Label = label;
            //unboundField.Settings.LabelPresenterStyle
            unboundField.Settings.AllowEdit = false;
            unboundField.Settings.CellClickAction = CellClickAction.SelectRecord;
            int indexName = this.grdQuotationComponent.FieldLayouts[0].Fields.IndexOf(name);
            this.grdQuotationComponent.FieldLayouts[0].Fields.Add(unboundField);
        }
        //Add summary defintion field
        private void AddSummaryDefinitionField(string name)
        {
            SummaryDefinition summaryDef = new SummaryDefinition();
            summaryDef.SourceFieldName = name;
            summaryDef.Calculator = SummaryCalculator.Sum;
            //summaryDef.StringFormat = "{} {0:n}";
            summaryDef.Key = name;
            this.grdQuotationComponent.FieldLayouts[0].SummaryDefinitions.Add(summaryDef);
        }
        private void RemoveSummaryFields()
        {
            foreach (DataRow categoryCostSummaryDr in _categoryCostSummaryDt.Rows)
            {
                string summaryName = categoryCostSummaryDr["CostTypeName"].ToString();
                int summaryFieldIndex = this.grdQuotationComponent.FieldLayouts[0].Fields.IndexOf(summaryName);
                if (summaryFieldIndex != -1)
                {
                    this.grdQuotationComponent.FieldLayouts[0].Fields.RemoveAt(summaryFieldIndex);
                }
                SummaryDefinition summaryDefinition = this.grdQuotationComponent.FieldLayouts[0].SummaryDefinitions[summaryName];
                if (summaryDefinition != null)
                {
                    this.grdQuotationComponent.FieldLayouts[0].SummaryDefinitions.Remove(summaryDefinition);
                }
            }
        }
        #endregion
    }
}

抱歉!评论已关闭.