using System;
using System.Windows.Forms;
using System.Data;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
String xmlFilename = "MyFile.xml";
DataSet Ds = new DataSet();
private int rowIndex;
public Form1()
{
InitializeComponent();
if (System.IO.File.Exists(xmlFilename))
{
Ds.ReadXml(xmlFilename);
dataGridView1.DataSource = Ds.Tables[0];
Ds.Tables[0].TableName = "IDPSN";
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ReadOnly = true;
}
}
private void dataGridView1_Click(object sender, EventArgs e)
{
rowIndex = dataGridView1.CurrentRow.Index;
textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
DataRow r = Ds.Tables[0].NewRow();
r[0] = textBox1.Text;
r[1] = textBox2.Text;
r[2] = textBox3.Text;
r[3] = textBox4.Text;
r[4] = textBox5.Text;
Ds.Tables[0].Rows.Add(r);
Ds.WriteXml("MyFile.xml");
MessageBox.Show("تمت الاضافة بنجاح", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button2_Click(object sender, EventArgs e)
{
if (rowIndex == -1) return;
if (MessageBox.Show("تحديث?", "تحديث", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return;
DataRow r = Ds.Tables["IDPSN"].Rows[rowIndex];
r[0] = textBox1.Text;
r[1] = textBox2.Text;
r[2] = textBox3.Text;
r[3] = textBox4.Text;
r[4] = textBox5.Text;
Ds.WriteXml(xmlFilename);
MessageBox.Show("تمت التحديث بنجاح", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
rowIndex = -1;
dataGridView1.ClearSelection();
}
private void button3_Click(object sender, EventArgs e)
{
if (rowIndex == -1) return;
if (MessageBox.Show("حذف?", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return;
Ds.Tables[0].Rows[rowIndex].Delete();
Ds.WriteXml(xmlFilename);
MessageBox.Show("تمت الحذف بنجاح", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
rowIndex = -1;
dataGridView1.ClearSelection();
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Mobile='{0}'", textBox6.Text);
}
}
}