Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

2 février 2014 7 02 /02 /février /2014 01:39

I–Liste générique

1-ItemsSource

datagridClients.ItemsSource = GetClients();

 

<DataGrid Name="datagridClients" Height="120"/>

 

La méthode appelée est quelconque et renvoie une List<Client> , cela pourrait être une méthode d’une classe DAO

public MainWindow()

        {

            InitializeComponent();

 

            // List<Client>

            datagridClients.ItemsSource = GetClients();

 

        }

        public List<Client> GetClients()

        {

            SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=dbDemo;Integrated Security=SSPI;");

 

            List<Client> clients = new List<Client>();

 

            using (SqlCommand command = new SqlCommand("Select * from [Client]", connection))

            {

                try

                {

                    connection.Open();

 

                    using (SqlDataReader reader = command.ExecuteReader())

                    {

                        while (reader.Read())

                        {

                            Client client = new Client();

                            client.Id = Convert.ToInt32(reader["Id"]);

                            client.Name = Convert.ToString(reader["Name"]);

                            client.Email = Convert.ToString(reader["Email"]);

                            client.CategoryId = Convert.ToInt32(reader["CategoryId"]);

 

                            clients.Add(client);

                        }

                    }

                }

                catch(Exception ex)

                {

                    throw ex;

                }

                finally

                {

                    connection.Close();

                }

            }

 

            return clients;

        }

 

2-Avec DataContext

  datagridClients.DataContext = GetClients();

 

Dans le Xaml il faut spécifier ItemsSource sinon le datagrid n’affiche rien

  <DataGrid Name="datagridClients" Height="120" ItemsSource="{Binding}"/>

 

Astuces :

  • Convertir la liste en ObservableCollection

  datagridClients.DataContext  = new ObservableCollection<Client>(GetClients());

 

  • convertir la liste en CollectionViewSource

CollectionViewSource clientsView = new CollectionViewSource { Source = GetClients() };

datagridClients.DataContext = clientsView;

//ou plus rapide

datagridClients.DataContext = new CollectionViewSource { Source = GetClients() };

 

 

II-DataTable/DataSet

1-ItemsSource

datagridClients.ItemsSource = GetClientsTable().AsDataView();

 

<DataGrid Name="datagridClients" Height="120"/>

 

 

public MainWindow()

        {

            InitializeComponent();

 

            // DataTable

            datagridClients.ItemsSource = GetClientsTable().AsDataView();

        }

        public DataTable GetClientsTable()

        {

            SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=dbDemo;Integrated Security=SSPI;");

 

            DataTable tableClient = new DataTable();

            SqlDataAdapter adpater = new SqlDataAdapter("Select * from Client", connection);                         

                 adpater.Fill(tableClient);                                        

            return tableClient;

        }

    }

 

2-DataContext

datagridClients.DataContext = GetClientsTable();

 

<DataGrid Name="datagridClients" Height="120" ItemsSource="{Binding}"/>

 

Astuces :

  • Conversion de la DataTable en ObservableCollection, demande un peu de code

ObservableCollection<Client> obsClients = new ObservableCollection<Client>();

      foreach (DataRow row in GetClientsTable().Rows)

             obsClients.Add(new Client(Convert.ToInt32(row["Id"]), Convert.ToString(row["Name"]), Convert.ToString(row["Email"]), Convert.ToInt32(row["CategoryId"])));

 

datagridClients.DataContext = obsClients;

 

  • Convertir la DataTable en CollectionViewSource

CollectionViewSource clientsView = new CollectionViewSource { Source = GetClientsTable() };

datagridClients.DataContext = clientsView;

 

Partager cet article

Repost 0
Published by Romagny13 - dans C
commenter cet article

commentaires