C#中刷新DataGridView后Details控件文本框不更新问题求助
Hey Dan, great question! You’ve hit the nail on the head—your initial guess is exactly right. Let’s break down why this happens and how to fix it.
Why the Sync Breaks
Before your refresh, your DataGridView and Details text boxes were almost certainly bound to the same underlying data source (like a shared DataSet or DataTable). WinForms’ data binding relies on this shared source to sync controls: when you select a row in the grid, the data source’s "current row" updates, and all bound controls (including your text boxes) automatically reflect that change.
But when you refresh, you’re creating a brand-new DataSet ds and reassigning it to the DataGridView. Your Details text boxes are still linked to the old, original data source—they have no connection to this new DataSet, so they can’t pick up row selection changes anymore.
Fixes to Restore Sync
Here are three approaches, ordered from simplest to most robust:
1. Reuse Your Existing Data Source (Quick Fix)
Instead of creating a new DataSet every time you refresh, clear the existing data and refill it. This preserves the original binding links between your grid and text boxes:
// Declare a class-level DataSet to reuse (e.g., at the top of your form) private DataSet _recordsDataSet = new DataSet(); private void RefreshButton_Click(object sender, EventArgs e) { string text = userIDLabel1.Text; int currentUserID; int.TryParse(text, out currentUserID); string dgvconn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\n0740572\Projects\CW\CW\bin\Debug\Database1.mdf;Integrated Security=True"; string sql = "select * from Records where UserID = @CurrentUserID"; using (SqlConnection connection = new SqlConnection(dgvconn)) { SqlDataAdapter dataadapter2 = new SqlDataAdapter(sql, connection); dataadapter2.SelectCommand.Parameters.AddWithValue("@CurrentUserID", currentUserID); // Clear existing data instead of making a new DataSet if (_recordsDataSet.Tables.Contains("Records")) { _recordsDataSet.Tables["Records"].Clear(); } dataadapter2.Fill(_recordsDataSet, "Records"); } // No need to reassign DataSource/DataMember—binding is still intact! recordsDataGridView.ReadOnly = true; }
2. Rebind the Details Text Boxes (Manual Approach)
If you must create a new DataSet, you’ll need to re-bind each text box to the new data source after refreshing:
// After filling the new DataSet and assigning to the grid... recordsDataGridView.DataSource = ds; recordsDataGridView.DataMember = "Records"; // Rebind each text box to the new DataTable recordNameTextBox.DataBindings.Clear(); recordNameTextBox.DataBindings.Add("Text", ds.Tables["Records"], "RecordName"); recordDateTextBox.DataBindings.Clear(); recordDateTextBox.DataBindings.Add("Text", ds.Tables["Records"], "RecordDate"); // Repeat for all your Details text boxes
This works, but it’s tedious if you have many controls—so the first method is better for simplicity.
3. Use a BindingSource (Best Practice)
For the most robust and maintainable solution, use a BindingSource as an intermediary between your data and controls. It handles sync automatically, even when you update the data source:
Step 1: Initialize the BindingSource (in your form’s constructor)
private BindingSource _recordsBindingSource = new BindingSource(); public YourFormName() { InitializeComponent(); // Bind grid to the BindingSource recordsDataGridView.DataSource = _recordsBindingSource; // Bind Details text boxes to the BindingSource recordNameTextBox.DataBindings.Add("Text", _recordsBindingSource, "RecordName"); recordDateTextBox.DataBindings.Add("Text", _recordsBindingSource, "RecordDate"); // Add bindings for all other text boxes here }
Step 2: Refresh with the BindingSource
private void RefreshButton_Click(object sender, EventArgs e) { string text = userIDLabel1.Text; int currentUserID; int.TryParse(text, out currentUserID); string dgvconn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\n0740572\Projects\CW\CW\bin\Debug\Database1.mdf;Integrated Security=True"; string sql = "select * from Records where UserID = @CurrentUserID"; using (SqlConnection connection = new SqlConnection(dgvconn)) { SqlDataAdapter dataadapter2 = new SqlDataAdapter(sql, connection); dataadapter2.SelectCommand.Parameters.AddWithValue("@CurrentUserID", currentUserID); DataTable newRecordsTable = new DataTable(); dataadapter2.Fill(newRecordsTable); // Update the BindingSource's data—all controls sync automatically! _recordsBindingSource.DataSource = newRecordsTable; } recordsDataGridView.ReadOnly = true; }
This is the WinForms recommended approach—it eliminates manual binding management and ensures your controls stay in sync no matter how often you refresh.
内容的提问来源于stack exchange,提问作者Daniel Wood

