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

binding5-copie-2.png

 

 

On crée un constructeur dans la form détail qui reçoit le bindingSource de la form de départ

FListClient

public partial class FListClient : Form

    {

        public FListClient()

        {

            InitializeComponent();

        }

 

        DataSet dataSet;

        SqlConnection connection;

        SqlDataAdapter clientAdapter;

        BindingSource bindingSource;

 

        private void FListClient_Load(object sender, EventArgs e)

        {

            dataSet = new DataSet();

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

 

            clientAdapter = new SqlDataAdapter("Select * from [dbo].[Client]", connection);

            dataSet.Tables.Add(new DataTable("Client"));

            clientAdapter.Fill(dataSet.Tables["Client"]);

 

            bindingSource = new BindingSource();

            bindingSource.DataSource = dataSet;

            bindingSource.DataMember = "Client";

            bindingSource.BindingComplete += bindingSource_BindingComplete;

 

            clientDataGridView.DataSource = bindingSource;

        }

 

        void bindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)

        {

            if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null)

                e.Binding.BindingManagerBase.EndCurrentEdit();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            FDetailClient detailForm = new FDetailClient(bindingSource);

            detailForm.Show();

        }

 

    }

 

FDetailClient

public partial class FDetailClient : Form

    {

        private BindingSource bindingSource;

 

        public FDetailClient()

        {

            InitializeComponent();

        }

 

        private BindingSource bindingSourceFDetail;

 

        public FDetailClient(BindingSource bindingSourceFList)

        {

            InitializeComponent();

 

            bindingSourceFDetail = bindingSourceFList;

 

            lblClientId.DataBindings.Add("Text", bindingSourceFDetail, "Id",true);

            txtClientName.DataBindings.Add("Text", bindingSourceFDetail, "Name");

            txtClientEmail.DataBindings.Add("Text", bindingSourceFDetail, "Email");

 

 

 

            // on remplit la comboBox avec la liste des catégories                     

            DataSet dataSet = new DataSet();

            SqlDataAdapter categoryAdapter = new SqlDataAdapter("Select * from [dbo].[Category]", new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=dbDemo;Integrated Security=True"));

            dataSet.Tables.Add(new DataTable("Category"));

            categoryAdapter.Fill(dataSet.Tables["Category"]);

            cboClientCategory.DataSource = dataSet;

            cboClientCategory.DisplayMember = "Category.Name";

            cboClientCategory.ValueMember = "Category.Id";

            cboClientCategory.DataBindings.Add("SelectedValue", bindingSourceFDetail, "CategoryId");

 

       

        }    

}

         


 

 

 

  Astuces  

Supprimer des colonnes du DatagridView

// supprimer des colonnes inutiles du DatagridView

 clientDataGridView.Columns.Remove("Id");

 clientDataGridView.Columns.Remove("CategoryId");

 

 

Ajouter une DataGridViewComboBoxColumn liée aux données

binding6.png  

 

DataGridViewComboBoxColumn cboClientCategory = new DataGridViewComboBoxColumn();

          

// 1 on crée un BindingSource avec pour DataSource la table Category

BindingSource categoryBindingSource = new BindingSource();

categoryBindingSource.DataSource = dataSet;

categoryBindingSource.DataMember = "Category";

 

// 2 on affecte le bindingSource à la DataGridViewComboBoxColumn

cboClientCategory.DataSource = categoryBindingSource;

// lié à la propriété/clé étrangère CategoryId de la table Client

cboClientCategory.DataPropertyName = "CategoryId";

// on affiche la propriété Name de la table Category dans la DataGridViewComboBoxColumn

cboClientCategory.DisplayMember = "Name";

// ValueMemeber est réglé sur la clé primaire Id de la table Category

cboClientCategory.ValueMember = "Id";

clientDataGridView.Columns.Add(cboClientCategory);                    

 

Partager cet article

Repost 0
Published by Romagny13 - dans ADO.NET
commenter cet article

commentaires