public partial class Form1 : Form { private OleDbConnection connection; private string connectionString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=..\..\..\Database.mdb;"; private int currentStationIndex = 0; private List<object> uniqueStations; private DataTable allData; public Form1() { InitializeComponent(); Text = "Data Entry"; //dataGridView1.ColumnCount = 2; // dataGridView1.RowCount = 12; connection = new OleDbConnection(connectionString); LoadAllRecords(); } private void LoadAllRecords() { try { using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM TableName ORDER BY Station", connection)) { OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); allData = new DataTable(); adapter.Fill(allData); // Get unique stations uniqueStations = allData.AsEnumerable() .Select(row => row.Field<object>("Station")) .Distinct() .ToList(); } DisplayCurrentStation(); } catch (Exception ex) { MessageBox.Show($"Error loading records: {ex.Message}\n\nStack Trace: {ex.StackTrace}"); } } private void button1_Click(object sender, EventArgs e) { try { using (OleDbCommand cmd = new OleDbCommand("INSERT INTO TableName (Station, Offset, Elevation) VALUES (@Station, @Offset, @Elevation)", connection)) { connection.Open(); for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@Station", textBox1.Text); cmd.Parameters.AddWithValue("@Offset", dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value); cmd.Parameters.AddWithValue("@Elevation", dataGridView1.Rows[i].Cells[1].Value ?? DBNull.Value); cmd.ExecuteNonQuery(); } MessageBox.Show("Data saved successfully!"); // Increase station by the interval double currentStation = double.Parse(textBox1.Text); double interval = double.Parse(textBox2.Text); textBox1.Text = (currentStation + interval).ToString(); // Clear the DataGridView dataGridView1.Rows.Clear(); // Reload records LoadAllRecords(); } } catch (Exception ex) { MessageBox.Show($"Error saving data: {ex.Message}\n\nStack Trace: {ex.StackTrace}"); } finally { if (connection.State == ConnectionState.Open) connection.Close(); } } private void button2_Click(object sender, EventArgs e) { // Show previous station if (currentStationIndex > 0) { currentStationIndex--; DisplayCurrentStation(); } else { MessageBox.Show("You are at the beginning of the stations."); } } private void button3_Click(object sender, EventArgs e) { // Show next station if (currentStationIndex < uniqueStations.Count - 1) { currentStationIndex++; DisplayCurrentStation(); } else { MessageBox.Show("You have reached the end of the stations."); } } private void DisplayCurrentStation() { if (uniqueStations.Count > 0 && currentStationIndex >= 0 && currentStationIndex < uniqueStations.Count) { object currentStation = uniqueStations[currentStationIndex]; textBox1.Text = currentStation.ToString(); var stationData = allData.AsEnumerable() .Where(row => row.Field<object>("Station") == currentStation) .Select(row => new { Offset = row.Field<double>("Offset"), Elevation = row.Field<double>("Elevation") }).ToList(); dataGridView1.DataSource = stationData; } } private void Form1_Load(object sender, EventArgs e) { // dataGridView1.AutoGenerateColumns = true; dataGridView1.Columns[0].Width = 140; dataGridView1.Columns[1].Width = 140; } }
"SELECT * FROM TableName ORDER BY Station"
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)