如何保留表单上的 datagridview 信息并防止 .Show() 按钮创建新实例?

How can I retain datagridview information on a form and prevent a .Show() button from creating new instances?

我有两个表单(菜单、数据库)都带有数据网格视图。第一个窗体有一个按钮列,用于将行信息复制到另一个窗体上的 datagridview 并显示它。第一个表单上还有一个按钮,用于简单地打开先前打开的第二个表单的实例。我有两个主要问题。第一个是行复制,但是当我在第一个表单中的另一行上按下另一个列按钮时,它会打开一个新的第二个表单,而不是将其值添加到已经打开的表单的实例中。第二个问题是,一旦按下第二个表单顶部的 X 按钮,表单关闭,信息就会丢失。我不介意在应用程序关闭后丢失信息,但是,无论第二个表单关闭如何,我都想保留它。我曾尝试在表单关闭事件期间使用单例来保留实例和 Hide() 以防止第二个表单关闭,但是,我的代码可能缺少或有问题。

第一表格菜单

    //Open Database
    private void DB_Btn_Click(object sender, EventArgs e)
    {
        SendRow = false;
        DataGridViewRow DB_GridViewRow = new DataGridViewRow();
        DataTable DB_Table = new DataTable();
        Database DB = new Database(DB_GridViewRow, SendRow);
        DB = Database.GetInstance();
        DB.Show();
    }

    //Copy To Database

    private void LeadsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        SendRow = true;
        DataTable DB_Table = new DataTable();
        DataGridView DB_GridView = new DataGridView();

        if (!_DB.Visible)
        {
            _DB = new Database(LeadsDataGridView.Rows[e.RowIndex], SendRow);
            _DB.Show();
        }

        else
        {
            SendRow = false;
            _DB = Database.GetInstance();
            _DB.BringToFront();
        }
     }

二级数据库

    DataTable DB_Table;
    BindingList<DataPile> DB_GridViewList = new BindingList<DataPile>();

    private static Database _instance = null;

    public static Database GetInstance()
    {
        bool SendRow = new bool();
        DataGridViewRow DB_GridViewRow = new DataGridViewRow();


        if (_instance == null) _instance = new Database(DB_GridViewRow, SendRow);
        return _instance;
    }

    public Database()
    {
        InitializeComponent();
        InitDBTable();
        GetInstance();
        DB_GridView.DataSource = DB_GridViewList;
    }

    public Database(DataGridViewRow DB_GridViewRow, bool SendRow)
    {
        InitializeComponent();
        InitDBTable();
        DB_GridView.DataSource = DB_GridViewList;

        if (DB_GridViewList != null && SendRow == true)
        {
            DB_GridViewList.Add(new DataPile { BusinessName = (string)DB_GridViewRow.Cells[1].FormattedValue,
                                               PhoneNumber = (string)DB_GridViewRow.Cells[2].FormattedValue });
        }

     }

    public class DataPile
    {
        public string BusinessName { get; set; }
        public string PhoneNumber { get; set; }
        public string MobileNumber { get; set; }
        public string ContactName { get; set; }
        public string Email { get; set; }
        public string MonthlyRevenue { get; set; }
    }

    private void InitDBTable()
    {
        DB_Table = new DataTable("DB_GridTable");
        DB_Table.Columns.Add("Business Name", typeof(string));
        DB_Table.Columns.Add("Phone Number", typeof(string));
        DB_Table.Columns.Add("Mobile Number", typeof(string));
        DB_Table.Columns.Add("Contact Name", typeof(string));
        DB_Table.Columns.Add("Email", typeof(string));
        DB_Table.Columns.Add("Monthly Revenue", typeof(string));
        DB_GridView.DataSource = DB_Table;
    }

    private void Database_Load(object sender, EventArgs e)
    {

    }

    private void Database_FormClosing(object sender, FormClosingEventArgs e)
    {
        Hide();
        e.Cancel = true; // this cancels the close event.
    }
}

您的代码令人困惑,不需要使用 _instance。这不是我的首选代码,但这里有一个更直接的方法来帮助您入门。 您会注意到 _instance 现在完全消失了,因为主窗体始终可以通过 _DB 直接访问您的数据库。此外,您在不需要时创建了 DataTable、DataGridView 和 DataGridViewRows,因此已将其删除。最后,我创建了一个名为 UniqueID 的新行。只需确保此行每次都创建一个新 ID。这样一来,您的行中只有一份副本会被转移到新表格中。

如果需要,您将必须处理将更改传回主窗体的操作。

主要形式:

    bool SendRow;
    Database _DB = new Database();

    public Form2()
    {
        InitializeComponent();
        LeadsDataGridView.Rows.Add("1", "ABCD", "555-1234", "555-5555", "john doe", "jdoe@email.com", "1");
        LeadsDataGridView.Rows.Add("2", "EFGH", "555-9876", "555-1111", "jane white", "jwhite@email.com", "21");
    }

    //Copy To Database
    private void LeadsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //if (e.ColumnIndex == LeadsDataGridView.Columns.GetFirstColumn(DataGridViewElementStates.None, DataGridViewElementStates.ReadOnly).Index)
        //    DB_Btn_Click(sender, e);
        SendRow = true;
        DataTable DB_Table = new DataTable();
        DataGridView DB_GridView = new DataGridView();

        _DB.AddRow(LeadsDataGridView.Rows[e.RowIndex], SendRow);
        if (!_DB.Visible)
        {
            _DB.Show();
        }
        else
        {
            _DB.BringToFront();
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox1.Hide();
    }
}

数据库表格:

    public DataTable DB_Table;
    BindingList<DataPile> DB_GridViewList = new BindingList<DataPile>();

    public Database()
    {
        InitializeComponent();
        InitDBTable();
        DB_GridView.DataSource = DB_GridViewList;
    }

    public void AddRow(DataGridViewRow DB_GridViewRow, bool SendRow)
    {
        if (DB_GridViewList != null && SendRow == true)
        {
            foreach (DataGridViewRow dr in DB_GridView.Rows)
                if (dr.Cells[0].Value == DB_GridViewRow.Cells[0].Value)
                    return;
            DB_GridViewList.Add(new DataPile
            {
                UniqueID = (string)DB_GridViewRow.Cells[0].FormattedValue,
                BusinessName = (string)DB_GridViewRow.Cells[1].FormattedValue,
                PhoneNumber = (string)DB_GridViewRow.Cells[2].FormattedValue
            });
        }

    }

    public class DataPile
    {
        public string UniqueID { get; set; }
        public string BusinessName { get; set; }
        public string PhoneNumber { get; set; }
        public string MobileNumber { get; set; }
        public string ContactName { get; set; }
        public string Email { get; set; }
        public string MonthlyRevenue { get; set; }
    }

    private void InitDBTable()
    {
        DB_Table = new DataTable("DB_GridTable");
        DB_Table.Columns.Add("UniqueID", typeof(string));            
        DB_Table.Columns.Add("Business Name", typeof(string));
        DB_Table.Columns.Add("Phone Number", typeof(string));
        DB_Table.Columns.Add("Mobile Number", typeof(string));
        DB_Table.Columns.Add("Contact Name", typeof(string));
        DB_Table.Columns.Add("Email", typeof(string));
        DB_Table.Columns.Add("Monthly Revenue", typeof(string));
        DB_GridView.DataSource = DB_Table;
        // Hide UniqueID column
        //DB_GridView.Columns[0].Visible = false;
    }

    private void Database_FormClosing(object sender, FormClosingEventArgs e)
    {
        Hide();
        e.Cancel = true; // this cancels the close event.
    }
}