1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| public partial class Form1 : Form
{
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList itemNodes;
bool start = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Chargement du fichier xml à l'emplacement cible
xmlDoc.Load("XMLFile2.xml");
//Lecture par noeuds en référence aux balises ("//.../.../...").
itemNodes = xmlDoc.SelectNodes("//XxX/Xx/x");
//Bouclage sur chaque noeuds et séléctions des attributs et paramètres.
foreach (XmlNode itemNode in itemNodes)
{
XmlNode actifNode = itemNode.Attributes["actif"];
XmlNode nomNode = itemNode.Attributes["nom"];
XmlNode prenomNode = intemNode.Attributes ["prenom"];
XmlNode ageNode = itemNode.Attributes ["age"];
if (actifNode != null)
{
//bool pour les items à case-coche
bool actif = (actifNode.Value == "true") ? true : false;
dataGridView1.Rows.Add(actif, nomNode.Value, prenomNode.Value, ageNode.Value);}
}
//En passant le start à faux, ou évite un éventuel bouclage accidentel.
start = false;
}
private void opensearch_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
MessageBox.Show(" " + sr.ReadToEnd());
sr.Close();
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//Actif
if (!start)
{
int iC = e.ColumnIndex;
int iR = e.RowIndex;
XmlNode itemNode = itemNodes.Item(iR);
XmlNode nomNode = itemNode.Attributes[iC];
MessageBox.Show("Modification : " + dataGridView1.Rows[iR].Cells[iC].Value + ", " + iC + ", " + iR);
//On compte avec le compteur à iC + "2" car on déplace le curseur de deux colonnes pour modifier la valeur à partir du nom.
itemNode.Attributes[iC + 2].Value = dataGridView1.Rows[iR].Cells[iC].Value.ToString();
xmlDoc.Save("XMLFile412.xml");
}
}
//Facultatif
//DialogResult dialogResult = MessageBox.Show("Souhaitez-vous redémarrer l'application?", "Attention", MessageBoxButtons.YesNo);
//if (dialogResult == DialogResult.Yes)
//Application.Restart();
//Sortie de l'application.
private void quit_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Êtes-vous sûr de vouloir quitter l'application?", "Attention", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
Application.Exit();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
} |
Partager