You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 DataGridView to your form and name it dgvTubeBundle.
  • Add a Button control, name it btnSave, 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.0 for .accdb files (Access 2007+). For older .mdb files, use Microsoft.Jet.OLEDB.4.0.
  • Data Type Compatibility: Ensure TubeID in tblTubeBundle is a Long Integer to match the auto-number field in tblStdTube.
  • Auto-Generated Columns: Disabling AutoGenerateColumns ensures you have full control over column types and order.
  • Dropdown Behavior: The combo box will display the formatted TubeDiaThk text but store the underlying TubeID value, which maintains referential integrity with tblStdTube.

内容的提问来源于stack exchange,提问作者Nimish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:45:33