绑定数据网格中的Outofmemoryexception

时间:2014-02-06 10:00:12

标签: c# .net wpf xaml

我有一个outofmemoryexception。我试图从CRM超过7000个帐户检索并存入contratvuemodele列表。此列表将添加到contratvuemodele的observablecollection并绑定到datagrid。 但我的申请不起作用。 这是我的代码:

代码xaml:

<UserControl x:Class="GestionDeContrats_Offres_ClientsGUI.VueModele.UserControlGestionContrat"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             x:Name="GestionContrat"
             xmlns:local="clr-namespace:GestionDeContrats_Offres_ClientsGUI.VueModele"
             d:DesignHeight="300"  >
   <!-- <UserControl.DataContext>
        <local:GestionDeContratVueModele  />
    </UserControl.DataContext>
    -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="40"/>
            <RowDefinition />
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="320"/>
                <ColumnDefinition Width="40" />
            </Grid.ColumnDefinitions>
            <TextBox Name="searchtexbox" Grid.Column="0"/>
            <Image Grid.Column="1" Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/16_find.gif" />
        </Grid>
        <ToolBar Grid.Row="1" Name="toolbarcontrat">
            <Button    Name="btNewContrat"  Click="btNewContrat_Click">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/plusvert.jpg" />
                    <Label Content="Nouveau" Grid.Column="1"/>

                </Grid>
            </Button>

            <Button    Name="btCopierContrat" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/editcopy.png" />
                    <Label Content="Copier" Grid.Column="1"/>

                </Grid>
            </Button>
            <Button    Name="btSupprimerContrat" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/delgreen16.jpg" />
                    <Label Content="Supprimer" Grid.Column="1"/>
                </Grid>
            </Button>
            <Button    Name="btModifierContrat" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/ico_18_4207.gif" />
                    <Label Content="Modifier" Grid.Column="1"/>
                </Grid>
            </Button>

        </ToolBar>

        <DataGrid Name="listViewContrat" Grid.Row="2" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding Path=Lescontrats, Mode=OneWay}"  IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="True" CanUserAddRows="True" CanUserDeleteRows="True">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Nom du contrat" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=NOMDUCONTRAT, Mode=OneWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Date de début" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=DATEDEDEBUT, Mode=OneWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Date de fin"  >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=DATEDEFIN, Mode=OneWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Statut" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=LESTATUT,Mode=OneWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Statut avant" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text=""/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Grid.Row="3" Orientation="Horizontal">
            <Label Margin="2" Content=""/>
            <Button Content="Suivant" Name="btNext" Margin="2" />
            <Button Content="Précédent" Name="btPrevious" Margin="2"  />


        </StackPanel>



    </Grid>
</UserControl>

代码cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionDeContrats_Offres_ClientsGUI.VueModele;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using GestionDeContrats_Offres_Clients.GestionOffres;
using GestionDeContrats_Offres_Clients.GestionContrats;
using System.Windows.Input;
using GestionDeContrats_Offres_Clients.GestionModele;
using GestionDeContrats_Offres_ClientsGUI.crm;
using System.Data;

namespace GestionDeContrats_Offres_ClientsGUI.VueModele
{
    /// <summary>
    /// 
    /// </summary>
   public class GestionDeContratVueModele : VueModeleBase
    {

        private readonly ObservableCollection<ContratVueModele> contrats;
        private readonly ICollectionView collectionView;
        //private readonly PagingCollectionView pagingView;
        private GestionDeContrat gestiondecontrat;
        private ICommand activerContratCommand;
        private ICommand supprimerContratCommand;
        private ICommand modifierContratCommand;
        private ICommand copierContratCommand;
        private ICommand creerContratCommand;
        private ICommand nextPageCommand;
        private ICommand previousPageCommand;
        private int currentpage;



       /// <summary>
       /// Constructeur de la classe
       /// GestionDeContratVueModele 
       /// </summary>
        public GestionDeContratVueModele() {


            try
            {

                this.gestiondecontrat = new GestionDeContrat();

                List<ContratVueModele> maliste = new List<ContratVueModele>(10000);
                //maliste.Clear();

                // this.contrats.Clear();

                foreach (contract contrat in this.gestiondecontrat.ListeDeContrat())
                {
                    // this.contrats.Add(new ContratVueModele());
                    maliste.Add(new ContratVueModele() { NOMDUCONTRAT = contrat.title, DATEDEDEBUT = contrat.activeon.Value, DATEDEFIN = contrat.expireson.Value, LESTATUT = contrat.statecode.formattedvalue, LESTATUTAVANT = contrat.access_etatavant.name });
                    // this.contrats.Add(new ContratVueModele() { NOMDUCONTRAT = contrat.title, DATEDEDEBUT = contrat.activeon.Value, DATEDEFIN = contrat.expireson.Value, LESTATUT = contrat.statecode.formattedvalue,LESTATUTAVANT=contrat.access_etatavant.name });


                }
                this.contrats = new ObservableCollection<ContratVueModele>(maliste);

                this.collectionView = CollectionViewSource.GetDefaultView(this.contrats);
                if (this.collectionView == null)
                    throw new NullReferenceException("collectionView");

                // this.collectionView.CurrentChanged += new EventHandler(collectionView_CurrentChanged);

                // this.pagingView = new PagingCollectionView(this.contrats, 15);

                //if (this.pagingView == null)
                //throw new NullReferenceException("pagingView");
                //this.currentpage = this.pagingView.CurrentPage;
                //this.pagingView.CurrentChanged += new EventHandler(pagingView_CurrentChanged);
            }
            catch (System.Web.Services.Protocols.SoapException soapEx)
            {

                soapEx.Detail.OuterXml.ToString();

            }
            catch (System.OutOfMemoryException memoryException)
            {

                memoryException.InnerException.ToString();

            }
            catch (System.Reflection.TargetInvocationException targetex) {


                targetex.InnerException.ToString();
            }

        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        //void pagingView_CurrentChanged(object sender, EventArgs e)
        //{
        //    OnPropertyChanged("SelectedContrat");
        //    Dispose();
        //    //throw new NotImplementedException();
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gestiondecontrat"></param>
        public GestionDeContratVueModele(GestionDeContrat gestiondecontrat)
        {

            this.gestiondecontrat = gestiondecontrat;


            this.contrats = new ObservableCollection<ContratVueModele>();

            foreach (contract contrat in this.gestiondecontrat.ListeDeContrat())
            {

                this.contrats.Add(new ContratVueModele() { NOMDUCONTRAT=contrat.title,DATEDEDEBUT=contrat.activeon.Value, DATEDEFIN=contrat.expireson.Value,LESTATUT=contrat.statecode.formattedvalue});


            }
            this.collectionView = CollectionViewSource.GetDefaultView(this.contrats);
            if (this.collectionView == null)
                throw new NullReferenceException("collectionView");

            this.collectionView.CurrentChanged += new EventHandler(collectionView_CurrentChanged);

        }


       /// <summary>
       /// 
       /// </summary>
        public ICommand ActiverContratCommand {

            get
            {

                if (this.activerContratCommand == null)
                    this.activerContratCommand = new CommandeDeRelais(() => this.ActiverContrat(), () => this.CanActiverContrat());
                return this.activerContratCommand;
            }

        }

       /// <summary>
       /// 
       /// </summary>
        public ICommand CopierContratCommand
        {

            get
            {

                if (this.copierContratCommand == null)
                    this.copierContratCommand = new CommandeDeRelais(() => this.CopierContrat(), () => this.CanCopierContrat());
                return this.copierContratCommand;
            }

        }

       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        private bool CanCopierContrat()
        {

            return true;
            //throw new NotImplementedException();
        }
       /// <summary>
       /// 
       /// </summary>
        private void CopierContrat()
        {
            this.gestiondecontrat.copier();
            //throw new NotImplementedException();
        }

       /// <summary>
       /// 
       /// </summary>

        //public ICommand NextPageCommand {

        //    get {

        //        if(this.nextPageCommand==null)
        //            this.nextPageCommand = new CommandeDeRelais(() => this.NextPage(), () => this.CanNextPage());
        //        return nextPageCommand;

        //    }


        //}
       /// <summary>
       /// 
       /// </summary>
        //private void NextPage() {

        //    this.pagingView.MoveToNextPage();

        //}
       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        //private bool CanNextPage() {


        //    return this.pagingView.PageCount > 1;//true; //Todo à reimplementer correctement
        //}


       /// <summary>
       /// Permet de gérer le passage à la page précedente
       /// </summary>
        //private void PreviousPage()
        //{

        //    this.pagingView.MoveToPreviousPage();

        //}

       /// <summary>
        /// Vérifie si on peut passer à la page précédente
       /// </summary>
       /// <returns>un booléen qui nous dit si on peut passer à la commande précédente</returns>
        //private bool CanPreviousPage()
        //{


        //    return this.pagingView.MoveCurrentToPrevious();//true; //Todo à reimplementer correctement
        //}



       /// <summary>
       /// Lecteur qui prend en charge la commande associée au bouton 
       /// </summary>
        //public ICommand PreviousPageCommand
        //{

        //    get
        //    {
        //        if (this.previousPageCommand==null)
        //            this.previousPageCommand = new CommandeDeRelais(() => this.PreviousPage(), () => this.CanPreviousPage());
        //        return previousPageCommand;

        //    }


        //}

       /// <summary>
       /// Lecteur de la page courante
       /// </summary>

        public int CURRENTPAGE {

            get {

               return this.currentpage;
            }

        }

       /// <summary>
       /// Lecteur qui prend en charge la Commande qui s'exécute quand on appuie sur le bouton créer
       /// ce bouton sera lié à cette propriété
       /// </summary>
        public ICommand CreerContratCommand {

            get
            {

                if (this.creerContratCommand == null)
                    this.creerContratCommand = new CommandeDeRelais(() => this.CreerContrat(), () => this.CanCreerContrat());
                return this.creerContratCommand;
            }

        }

       /// <summary>
       /// Lecteur sur la commande supprimer le contrat
       /// </summary>
        public ICommand SupprimerContratCommand {

            get {

                if (this.supprimerContratCommand == null)
                    this.supprimerContratCommand = new CommandeDeRelais(() => this.SupprimerContrat(), () => this.CanSupprimerContrat());
                return this.supprimerContratCommand;

            }


        }
       /// <summary>
       /// 
       /// </summary>
        public ICommand ModifierContratCommand {

            get {
                if (this.modifierContratCommand == null)
                    this.modifierContratCommand = new CommandeDeRelais(() => this.ModifierContrat(), () => this.CanModifierContrat());
                return this.modifierContratCommand;

            }

        }

                /// <summary>
        /// Propriété permettant de manipuler la 
        ///Vue Modèle de la liste des contrats
        /// </summary>
        public ObservableCollection<ContratVueModele> Lescontrats
        {

            get
            {

                return this.contrats;
            }


        }



        /// <summary>
        /// Propriété permettant de manipuler la vue modèle
        /// de selection du contrat
        /// </summary>
        public ContratVueModele SelectedContrat
        {
            get
            {

                return this.collectionView.CurrentItem as ContratVueModele;

            }

        }


        /// <summary>
        /// Propriété permettant manipuler la vue modèle qui vérifie si 
        /// que la vue est vide
        /// </summary>
        public bool SearchContainsNoMatch
        {

            get
            {


                return this.collectionView.IsEmpty;
            }

        }

        /// <summary>
        /// Propriété permettant manipuler la vue modèle 
        /// de recherche du contrat en fonction du nom ou du statut
        /// </summary>
        public string RechercheText
        {

            set
            {

                this.collectionView.Filter = (item) =>
                    {

                        if (item as ContratVueModele == null)
                            return false;

                        ContratVueModele contratvueModele = (ContratVueModele)item;
                        if (contratvueModele.NOMDUCONTRAT.Contains(value) || contratvueModele.LESTATUT.Contains(value))
                            return true;
                        return false;
                    };
                this.OnPropertyChanged("SearchContainsNoMatch");
            }

        }

        /// <summary>
        /// Handler pour la sélection du contrat
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void collectionView_CurrentChanged(object sender, EventArgs e)
        {

            OnPropertyChanged("SelectedContrat");
            //Dispose();
            //throw new NotImplementedException();
        }


        /// <summary>
        /// 
        /// </summary>
        private void CreerContrat() {

            try
            {
                 this.gestiondecontrat.creer();
                 foreach (contract contrat in this.gestiondecontrat.ListeDeContrat())
                 {

                     this.contrats.Add(new ContratVueModele() { NOMDUCONTRAT=contrat.title, DATEDEDEBUT=contrat.activeon.Value, DATEDEFIN=contrat.expireson.Value, ADRESSE=contrat.serviceaddress.name, NET=contrat.netprice.Value});


                 }

            }
            catch(Exception creerEx){

                creerEx.Message.ToString();

            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CanCreerContrat() {

            return true;

        }
       /// <summary>
       /// 
       /// </summary>
        private void ActiverContrat()
        {

            try
            {
                this.gestiondecontrat.ActiverContrat(this.SelectedContrat.CONTRAT);


            }
            catch (Exception activerEx)
            {

                activerEx.Message.ToString();

            }

        }
       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        private bool CanActiverContrat()
        {

            return this.SelectedContrat.LESTATUT != "Annulé";

        }

       /// <summary>
       /// 
       /// </summary>
        private void SupprimerContrat()
        {

            try
            {
                this.gestiondecontrat.supprimer();
                this.contrats.Remove(new ContratVueModele(this.SelectedContrat.CONTRAT));
            }
            catch (Exception supprimerEx)
            {

                supprimerEx.Message.ToString();

            }

        }

       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        private bool CanSupprimerContrat()
        {

            return this.contrats.Count>0;//true;

        }

       /// <summary>
       /// 
       /// </summary>
        private void ModifierContrat() {


            this.gestiondecontrat.modifier();


        }
       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        private bool CanModifierContrat() {


            return this.SelectedContrat != null;

        }

       /// <summary>
       /// 
       /// </summary>
        private void OuvrirDocumentContrat()
        {

            try
            {
               // Contrat newContrat = this.gestiondecontrat.CreerContrat(new Contrat(new ModeleDeContrat()));
                //this.contrats.Add(new ContratVueModele(newContrat));
            }
            catch (Exception OuvrirDocumentContratEx)
            {

                OuvrirDocumentContratEx.Message.ToString();

            }

        }


       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        private bool CanOuvrirDocumentContrat()
        {

            return true;

        }



    }
}

背后的代码

using System;
using System.Collections.Generic;
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;

namespace GestionDeContrats_Offres_ClientsGUI.VueModele
{
    /// <summary>
    /// Logique d'interaction pour UserControlGestionContrat.xaml
    /// </summary>
    public partial class UserControlGestionContrat : UserControl
    {
        private readonly GestionDeContratVueModele vuemodelecontrat;

        /// <summary>
        /// 
        /// </summary>
        public UserControlGestionContrat()
        {
            InitializeComponent();
            this.vuemodelecontrat = new GestionDeContratVueModele();
            this.DataContext = this.vuemodelecontrat;
        }
    }     
}

在我的MainWindow.xaml中,我只是集成了所有usercontrol

由于

祝你好运

1 个答案:

答案 0 :(得分:0)

我通过使用listview解决了这个问题的一部分,对设计进行了一些更改。我做了一些方法,并在我的构造函数中调用它,现在添加列表正在进入这些方法。我也改变了绑定模式。

现在我有另一个问题。 当我在visual studio上运行我的解决方案时,需要花费大量时间超过半小时。它消耗了许多资源。它没有正确完成。