Ejemplo n.º 1
0
        private void ActPrimaryKey_Click(System.Object sender, System.EventArgs e)
        {
            // ----- Perform a primary key lookup.
            long      usePrimaryKey;
            DataRow   result = null;
            DataTable workTable;
            DataTable displayTable;

            // ----- The primary key is required, and must be an integral value.
            if ((IsNumeric(PrimaryKey.Text.Trim()) == false) ||
                (PrimaryKey.Text.IndexOf(".") >= 0))
            {
                MessageBox.Show("The Primary Key must be a valid integer.");
                PrimaryKey.Focus();
                return;
            }

            // ----- Convert supplied key to long, checking for overflow.
            try
            {
                usePrimaryKey = long.Parse(PrimaryKey.Text);
            }
            catch
            {
                MessageBox.Show("The Primary Key must be a valid integer.");
                PrimaryKey.Focus();
                return;
            }

            // ----- Get a copy of the table for the lookup.
            workTable = BuildSampleTable();

            // ----- Perform the lookup.
            try
            {
                result = workTable.Rows.Find(usePrimaryKey);
            }
            catch (MissingPrimaryKeyException)
            {
                MessageBox.Show("The table does not have a defined primary key.");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Primary key lookup failed with the following error: " +
                                ex.Message);
                return;
            }

            // ----- Check for no match.
            if (result == null)
            {
                MessageBox.Show("The supplied key matched no rows.");
                PrimaryKey.Focus();
                return;
            }

            // ----- Display the results. The results panel requires a DataTable,
            //       so build a new table that includes only the matching row.
            displayTable = workTable.Clone();
            displayTable.ImportRow(result);
            (new ResultsViewer()).ShowResults(displayTable);
        }