C# WinForm中DataGridView绑定ComboBox列的实现求助
Step-by-Step Setup for DataGridView Dropdown Column
Here's how to implement the dropdown column for your TubeID field in the DataGridView:
1. Form Setup
- Add a
DataGridViewto your form and name itdgvTubeBundle. - Add a
Buttoncontrol, name itbtnSave, and set its text to "Save Changes".
2. Load Data and Configure Dropdown Column
Paste this code into your form's Load event handler:
using System; using System.Windows.Forms; using System.Data; using System.Data.OleDb; private void Form1_Load(object sender, EventArgs e) { // Update this path to match your Access database location string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\YourDatabase.accdb;Persist Security Info=False;"; // Load tube specifications for the dropdown string tubeQuery = "SELECT TubeID, TubeOutsideDiameter & ' x ' & TubeThickness AS TubeDiaThk FROM tblStdTube"; DataTable tubeData = new DataTable(); using (OleDbConnection conn = new OleDbConnection(connString)) { new OleDbDataAdapter(tubeQuery, conn).Fill(tubeData); } // Create the dropdown column for TubeID DataGridViewComboBoxColumn tubeCombo = new DataGridViewComboBoxColumn { Name = "TubeID", DataPropertyName = "TubeID", // Binds to tblTubeBundle's TubeID field DisplayMember = "TubeDiaThk", // Shows the combined diameter/thickness text ValueMember = "TubeID", // Stores the actual TubeID value DataSource = tubeData, HeaderText = "Tube Specification" }; // Load tube bundle data string bundleQuery = "SELECT TubeBundleID, TubeID, TubePitch FROM tblTubeBundle"; DataTable bundleData = new DataTable(); using (OleDbConnection conn = new OleDbConnection(connString)) { new OleDbDataAdapter(bundleQuery, conn).Fill(bundleData); } // Configure DataGridView dgvTubeBundle.AutoGenerateColumns = false; dgvTubeBundle.DataSource = bundleData; // Add columns manually dgvTubeBundle.Columns.AddRange( new DataGridViewTextBoxColumn { Name = "TubeBundleID", DataPropertyName = "TubeBundleID", HeaderText = "Bundle ID", ReadOnly = true // Auto-number field shouldn't be edited }, tubeCombo, new DataGridViewTextBoxColumn { Name = "TubePitch", DataPropertyName = "TubePitch", HeaderText = "Tube Pitch" } ); }
3. Add Save Functionality
Add this code to the Click event handler of your btnSave button to save changes back to the database:
private void btnSave_Click(object sender, EventArgs e) { string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\YourDatabase.accdb;Persist Security Info=False;"; string bundleQuery = "SELECT TubeBundleID, TubeID, TubePitch FROM tblTubeBundle"; using (OleDbConnection conn = new OleDbConnection(connString)) { OleDbDataAdapter adapter = new OleDbDataAdapter(bundleQuery, conn); OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); DataTable bundleTable = (DataTable)dgvTubeBundle.DataSource; adapter.Update(bundleTable); MessageBox.Show("Changes saved!"); } }
Important Notes
- Connection String: Use
Microsoft.ACE.OLEDB.12.0for .accdb files (Access 2007+). For older .mdb files, useMicrosoft.Jet.OLEDB.4.0. - Data Type Compatibility: Ensure
TubeIDintblTubeBundleis a Long Integer to match the auto-number field intblStdTube. - Auto-Generated Columns: Disabling
AutoGenerateColumnsensures you have full control over column types and order. - Dropdown Behavior: The combo box will display the formatted
TubeDiaThktext but store the underlyingTubeIDvalue, which maintains referential integrity withtblStdTube.
内容的提问来源于stack exchange,提问作者Nimish
相关产品推荐
相关产品推荐

