Untitled
unknown
plain_text
a year ago
4.0 kB
15
Indexable
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace DataGridViewDemo
{
public partial class PhoneBookFrm : Form
{
static AppData App = new AppData(); // Create an instance of AppData
public PhoneBookFrm()
{
InitializeComponent(); // Initialize form components
}
private void PhoneBookFrm_Load(object sender, EventArgs e)
{
string filePath = string.Format("{0}//data.dat", Application.StartupPath);
if (File.Exists(filePath))
App.PhoneBook.ReadXml(filePath); // Load data if file exists
phoneBookBindingSource.DataSource = App.PhoneBook; // Bind the data source
pnl.Enabled = false; // Disable panel initially
}
private void bttnNew_Click(object sender, EventArgs e)
{
pnl.Enabled = true;
App.PhoneBook.AddPhoneBookRow(App.PhoneBook.NewPhoneBookRow());
phoneBookBindingSource.MoveLast();
txtFullName.Focus();
}
private void bttnEdit_Click(object sender, EventArgs e)
{
pnl.Enabled = true;
txtFullName.Focus();
}
private void bttnCancel_Click(object sender, EventArgs e)
{
phoneBookBindingSource.ResetBindings(false);
pnl.Enabled = false;
}
private void bttnSave_Click(object sender, EventArgs e)
{
try
{
phoneBookBindingSource.EndEdit();
App.PhoneBook.AcceptChanges();
App.PhoneBook.WriteXml(string.Format("{0}//data.dat", Application.StartupPath));
pnl.Enabled = false;
MessageBox.Show("Record Saved Successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
App.PhoneBook.RejectChanges();
}
}
private void bttnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete this record?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
phoneBookBindingSource.RemoveCurrent();
App.PhoneBook.AcceptChanges();
App.PhoneBook.WriteXml(string.Format("{0}//data.dat", Application.StartupPath));
}
}
private void txtBxSearch_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (!string.IsNullOrEmpty(txtBxSearch.Text))
{
var result = from phoneBook in App.PhoneBook
where !phoneBook.IsNull(0) &&
(phoneBook.PhoneNumber == txtBxSearch.Text ||
phoneBook.FullName.Contains(txtBxSearch.Text) ||
phoneBook.Email == txtBxSearch.Text)
select phoneBook;
dtGrdVw.DataSource = result.ToList();
return;
}
}
else if (e.KeyCode == Keys.Escape)
txtBxSearch.Clear();
else
return;
dtGrdVw.DataSource = phoneBookBindingSource;
}
private void dtGrdVw_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
foreach (DataGridViewRow row in dtGrdVw.SelectedRows)
App.PhoneBook.Rows.Remove((row.DataBoundItem as DataRowView).Row);
App.PhoneBook.AcceptChanges();
}
}
}
}
Editor is loading...
Leave a Comment