CNSA-212-Personal/Database3/frmSearch.cs
2024-02-15 12:04:47 -05:00

143 lines
4.1 KiB
C#

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Database3
{
public partial class frmSearch : Form
{
public static string myID = "";
public frmSearch()
{
InitializeComponent();
dgvStudents.DoubleClick += new EventHandler(dgvStudents_DoubleClick);
}
private void dgvStudents_DoubleClick(object sender, EventArgs e)
{
string studentid = "";
if (dgvStudents.Rows.Count > 0)
{
DataGridViewRow row = dgvStudents.SelectedRows[0];
frmEdit aform = new frmEdit(this);
studentid = row.Cells[0].Value.ToString().Trim();
myID = studentid;
aform.ShowDialog();
}
}
public void btnSearch_Click(object sender, EventArgs e)
{
string studentid = "9999999999";
string lname = "9999999999";
DateTime dob = new DateTime();
ErrorProvider ep1 = new ErrorProvider();
try
{
DataSet ds = new DataSet();
StudentDataTier stuDT = new StudentDataTier();
studentid = txtStuID.Text;
lname = txtlname.Text;
try
{
dob = DateTime.Parse(txtDOB.Text);
}
catch (Exception exception)
{
ep1.SetError(txtDOB, "Non valid value, but that's ok");
}
if (string.IsNullOrWhiteSpace(txtStuID.Text) &&
string.IsNullOrWhiteSpace(txtlname.Text) &&
string.IsNullOrWhiteSpace(txtDOB.Text))
{
throw new Exception("Must Enter at least one value");
}
// Get the dataset using all three search parameters.
ds = stuDT.GetStudents(studentid, lname, dob);
// Check to see if any record is returned.
if (ds.Tables[0].Rows.Count > 0) // There is a record.
{
dgvStudents.Visible = true;
// Get data source.
dgvStudents.DataSource = ds.Tables[0];
dgvStudents.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen;
// Set the row and column header styles.
dgvStudents.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dgvStudents.ColumnHeadersDefaultCellStyle.BackColor = Color.Green;
}
else
{
dgvStudents.Visible = false; // Hide the DataGridView if no results are found.
MessageBox.Show("No records found.");
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}
}
private void frmSearch_Load(object sender, EventArgs e)
{
dgvStudents.Visible = false;
contextMenuStrip1.Items[1].Enabled = false;
}
private void cmuSearch_Click(object sender, EventArgs e)
{
btnSearch_Click(sender,e);
}
private void txtStuID_TextChanged(object sender, EventArgs e)
{
cmuSearch.Enabled = txtStuID.Text.Length>0;
}
private void cmuEdit_Click(object sender, EventArgs e)
{
dgvStudents_DoubleClick(sender, e);
}
private void cmuDelete_Click(object sender, EventArgs e)
{
string studentid = "";
DataSet ds = new DataSet();
StudentDataTier stuDT = new StudentDataTier();
DataGridViewRow row = dgvStudents.SelectedRows[0];
studentid = row.Cells[0].Value.ToString().Trim();
stuDT.DeleteStudents(studentid);
btnSearch_Click(sender, e);
}
}
}