请求帮助:实现Form2点击行后Form1的DataGridView永久显示数据
Hey there! Let's figure out how to fix that issue where your Form1's DataGridView doesn't show the selected row data from Form2 until you reload it. The core problem here is likely how you're navigating between forms and updating the data—let's break down the solution step by step.
Root Cause
When you double-click a row in Form2 and go back to Form1, you're probably creating a new instance of Form1 instead of updating the original one. That's why the original Form1 doesn't reflect the new data until you reload it (which creates another fresh instance).
Step-by-Step Solution
We'll fix this by passing a reference of the original Form1 to Form2, then using that reference to update Form1's DataGridView directly when a row is double-clicked.
- Pass Form1's instance to Form2: Give Form2 context about which Form1 window to update later.
- Add a public update method in Form1: Create a method that accepts selected row data and refreshes the grid.
- Handle double-click in Form2: Grab the selected row and trigger Form1's update method.
Full Code Examples
Form1 Code
public partial class Form1 : Form { private DataTable _form1DataTable; public Form1() { InitializeComponent(); // Initialize your DataGridView and data source _form1DataTable = new DataTable(); dataGridView1.DataSource = _form1DataTable; } // Button click to open Form2 private void btnOpenForm2_Click(object sender, EventArgs e) { // Pass the current Form1 instance to Form2 Form2 form2 = new Form2(this); form2.Show(); this.Hide(); // Optional: Hide Form1 while Form2 is open } // Public method to update Form1's DataGridView with selected data public void RefreshGridWithSelectedRow(DataRow selectedRow) { // Add the selected row to Form1's data table (adjust logic for your use case) _form1DataTable.ImportRow(selectedRow); // Force grid refresh (in case auto-refresh doesn't trigger) dataGridView1.Refresh(); // Show Form1 again this.Show(); } }
Form2 Code
public partial class Form2 : Form { private Form1 _originalForm1; private DataTable _form2DataTable; // Constructor that accepts the original Form1 instance public Form2(Form1 parentForm) { InitializeComponent(); _originalForm1 = parentForm; } // Button click to load database data into Form2's DataGridView private void btnLoadDatabase_Click(object sender, EventArgs e) { // Replace this with your actual database loading logic _form2DataTable = new DataTable(); using (var conn = new SqlConnection("YourDatabaseConnectionString")) { string query = "SELECT * FROM YourTargetTable"; var adapter = new SqlDataAdapter(query, conn); adapter.Fill(_form2DataTable); } dataGridView2.DataSource = _form2DataTable; } // Double-click event for Form2's DataGridView rows private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { // Ensure we're clicking a valid row (not the header) if (e.RowIndex >= 0) { // Get the selected row from Form2's data source DataRow selectedRow = _form2DataTable.Rows[e.RowIndex]; // Call Form1's method to update its grid _originalForm1.RefreshGridWithSelectedRow(selectedRow); // Close Form2 this.Close(); } } }
Key Notes
- No new Form1 instances: We're using the original Form1 reference instead of creating
new Form1()—this ensures the existing window updates immediately. - Data binding: Make sure Form1's DataGridView is bound to a live data source (like a DataTable) so changes reflect automatically.
- Customize logic: Adjust the
RefreshGridWithSelectedRowmethod to match your needs—you might want to update an existing row instead of adding a new one, or use a different data structure.
This should resolve your issue—next time you return to Form1, the selected row data will show up in its DataGridView right away, no reload needed!
内容的提问来源于stack exchange,提问作者Mahjoubi houssem

