private void toolStripButton1_Click(object sender, EventArgs e) { MessageBox.Show(this.contactsDataSet.TYPE_CONTACT.Rows[tYPE_CONTACTDataGridView.CurrentRow.Index].RowState.ToString()); }
DataRow [] oDataRow; // On sélectionne la ligne (ici grâce à la clé primaire) oDataRow = this.contactsDataSet.TYPE_CONTACT.Select( "code_type_contact='" + this.tYPE_CONTACTDataGridView.CurrentRow.Cells[0].Value.ToString() + "'" ); textBox1.Text = string.Empty; textBox2.Text = string.Empty; if (oDataRow[0].HasVersion( DataRowVersion.Original) == true ) { textBox1.Text = oDataRow[0][1, DataRowVersion.Original].ToString(); } if (oDataRow[0].HasVersion( DataRowVersion.Current) == true ) { textBox2.Text = oDataRow[0][1, DataRowVersion.Current].ToString(); }
oDataRow[0].SetAdded();
oDataRow[0].SetModified();
oDataRow[0].Delete();
oDataRow[0].GetChildRows();
oDataRow[0].ClearErrors();
Exemple : Supprimer toutes les erreurs des lignes d'une DataTable
foreach (DataRow oDataRow in contactsDataSet.TYPE_CONTACT.Rows) { oDataRow.ClearErrors(); }
// Declaration DataRow Type_Contact TestReference.ContactsDataSet.TYPE_CONTACTRow oDataRow; // oDataRow = this.contactsDataSet.TYPE_CONTACT.NewTYPE_CONTACTRow(); // Définition des propriétés oDataRow.libelle_type_contact = "oui"; // Ajout de la DataRow a la collection de DataRow de la DataTable > 2 surcharges pour cette méthode this.contactsDataSet.TYPE_CONTACT.AddTYPE_CONTACTRow(oDataRow);
TestReference.ContactsDataSet.TYPE_CONTACTRow oDataRow; oDataRow = this.contactsDataSet.TYPE_CONTACT.FindBycode_type_contact(Convert.ToInt32(this.tYPE_CONTACTDataGridView.CurrentRow.Cells[0].Value)); this.contactsDataSet.TYPE_CONTACT.RemoveTYPE_CONTACTRow(oDataRow);
Pour trouver un élément par sa « clé » rapidement
private void toolStripButton1_Click(object sender, EventArgs e) { DataRow oDataRow; oDataRow = this.contactsDataSet.TYPE_CONTACT.NewTYPE_CONTACTRow(); oDataRow = this.contactsDataSet.TYPE_CONTACT.FindBycode_type_contact(Convert.ToInt32(this.tYPE_CONTACTDataGridView.CurrentRow.Cells[0].Value)); textBox1.Text = oDataRow[1].ToString(); }
AcceptChanges() permet de valider les changements apportés , l'etat RowState des lignes passe à unchanged
RejectChanges() permet d'annuler les changements apportés : les lines reprennent leur valeur Original
Ces 2 méthodes s'appliquent pour les DataSet, les DataTable, les DataRow
private void toolStripButton1_Click(object sender, EventArgs e) { DataTable oDataTable; oDataTable = newDataTable(); if (this.contactsDataSet.TYPE_CONTACT.HasErrors == true) { foreach (DataRow oDataRow in this.contactsDataSet.TYPE_CONTACT.Rows) { if (oDataRow.RowError != string.Empty) { oDataTable.Rows.Add(oDataRow); } } } dataGridView1.DataSource = oDataTable; }
Persistance.OleDb.CONTACT oContact; oContact = new Persistance.OleDb.CONTACT(); Persistance.OleDbConnecte.sCONNECTIONSTRING = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Documents and SettingsromagnyMes documentsContacts.mdb"; System.Data.DataTable oDataTable; oDataTable = newDataTable(); oDataTable.Load(oContact.ChargerCONTACTs()); //oContact.ChargerCONTACTs() est une méthode retournant un datareader // Affichage comboBox1.DataSource = oDataTable; comboBox1.ValueMember = "nom_contact";
System.Data.Common.DbDataReader oDbDataReader; oDbDataReader = oDataTable.CreateDataReader(); while (oDbDataReader.Read()) { comboBox2.Items.Add(oDbDataReader["nom_contact"]); } oDbDataReader.Close();
Méthode disponible uniquement pour le DataSet. Elle permet de voir si des lignes ont été ajoutées, modifies ou supprimées
Le but étant en fait d'utiliser cette méthode pour faire un test afin de n'exécuter la mise à jour que si des changements ont été apportés :
this.Validate(); this.TYPE_CONTACT.BindingSource.EndEdit(); if (this.contacts.DataSet.HasChanges() == true) { this.TYPE_CONTACT.TableAdapter.Update(this.contactsDataSet.TYPE_CONTACT); DataTable oDataTable = this.contacts.DataSet.TYPE_CONTACT.GetChanges(); dataGridView1.DataSource = oDataTable; } else { MessageBox.Show("Aucune modification"); }
// Filtrer selon l'etat des DataRow (DataRowState) DataSet ds = contactsDataSet.GetChanges(DataRowState.Added); DataSet ds = contactsDataSet.GetChanges(DataRowState.Deleted); DataSet ds = contactsDataSet.GetChanges(DataRowState.Modified); DataSet ds = contactsDataSet.GetChanges(DataRowState.Unchanged); // ici par exemple on met en évidence toutes les lignes // en affichant que pour les lignes "modified" DataSet ds = contacts.DataSet.GetChanges(DataRowState.Modified); dataGridView1.DataSource = ds.Tables["TYPE_CONTACT"];
Récupérer et afficher les lignes n'ayant pu être mise à jour:
try { this.Validate(); this.TYPE_CONTACT.BindingSource.EndEdit(); this.TYPE_CONTACT.TableAdapter.Update(this.contactsDataSet.TYPE_CONTACT); DataTable oDataTable = this.contacts.DataSet.TYPE_CONTACT.GetChanges(); dataGridView1.DataSource = oDataTable; }
Permet de continuer la mise à jour de l'ensemble des lignes même si des erreurs sont rencontrées. Sinon, si cette propriété du TableAdapter n'est pas défini à true, une exception est levée à la première erreur et la mise à jour est interrompue.
Il faut indiquer cela juste avant l'appel à la méthode Update()
Pour un TableAdapter défini par Visual Studio (avec un DataSet typé) il faut faire une « mini » acrobatie et aller placer cette ligne de code dans l'appel de la méthode Update:
[System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")] public virtual int Update(Contacts.DataSet.TYPE_CONTACT.DataTable dataTable) { this.Adapter.ContinueUpdateOnError = true; return this.Adapter.Update(dataTable); }
On récupère les lignes n'ayant pas été mises à jour grâce à la méthode GetChanges() de la DataTable concernée. Cette méthode s'applique également aux DataSet et aux DataRow (ici en plus j'affiche la liste des lignes en erreur dans un DataGridView)
this.Validate(); this.TYPE_CONTACT.BindingSource.EndEdit(); this.TYPE_CONTACT.TableAdapter.Update(this.contactsDataSet.TYPE_CONTACT); DataTable oDataTable = this.contactsDataSet.TYPE_CONTACT.GetChanges(); dataGridView1.DataSource = oDataTable;
Interaction Office [Excel] - ADO.NET: http://romagny13.over-blog.com/article-5524382.html
DatagridView (personnalisation): http://msdn.microsoft.com/en-us/library/ms171618.aspx