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

求助:如何根据数据表为DataGridView动态生成关联的ComboBox列?

Fixing DataGridView ComboBox Column Mapping to Status Data

Hey there, let's sort out your DataGridView ComboBox issue. I see you're trying to map the stat column values to their corresponding descriptions from the sdg.pa_status table, but there are a few key mistakes in how you're setting up the ComboBox column and integrating it with your DataGridView. Let's break down the fixes step by step:

Key Issues in Your Current Code

  • Incorrect DataPropertyName: You're using dGVQuote.Columns["stat"].ToString() which returns the column's string representation instead of just the column name "stat". This prevents the ComboBox from binding to the correct value in your main data table.
  • Wrong ValueMember and DisplayMember: Using _status.Columns[1].ToString() doesn't give you the column name; instead, reference the actual column names from your status table ("id" for ValueMember and "description" for DisplayMember).
  • Re-setting DataSource After Adding Column: You set the DataGridView's DataSource twice, and add the ComboBox column in between—this can cause the column to not bind properly. Also, you should hide the original stat column since you're replacing it with the ComboBox.
  • Unreadable FlatStyle Assignment: Using FlatStyle = 0 is better replaced with the enum value for clarity.

Corrected Code Implementation

First, fix the ComboBox column creation:

private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
    DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
    column.Name = "status_column"; // Unique name to avoid conflict with original "stat" column
    column.DropDownWidth = 160;
    column.Width = 160;
    column.FlatStyle = FlatStyle.Standard; // Use enum instead of magic number for readability
    column.HeaderText = "Status";
    
    // Bind to your pre-populated status data table
    column.DataSource = _status;
    column.ValueMember = "id"; // Maps to the ID value in pa_status
    column.DisplayMember = "description"; // Shows the friendly text to users
    column.DataPropertyName = "stat"; // Links directly to the "stat" column in your main data table
    
    return column;
}

Then, update the CustomizeDataGridViewOrder method:

private void CustomizeDataGridViewOrder() 
{
    // Set the DataSource once at the start to avoid resetting columns
    dGVQuote.DataSource = GetQuote("");
    
    // Configure existing columns as needed
    dGVQuote.Columns["ID"].Width = 135;
    dGVQuote.Columns["ID"].HeaderText = "Angebotsnummer";
    dGVQuote.Columns["idorder"].Visible = false;
    dGVQuote.Columns["Description"].Width = 225;
    dGVQuote.Columns["Description"].HeaderText = "Beschreibung";
    dGVQuote.Columns["comment"].Width = 225;
    dGVQuote.Columns["comment"].HeaderText = "interner Kommentar";
    dGVQuote.Columns["idcust"].Visible = false;
    dGVQuote.Columns["idobj"].Visible = false;
    dGVQuote.Columns["cnt"].Width = 60;
    dGVQuote.Columns["cnt"].HeaderText = "Anzahl MA";
    
    // Hide the original numeric "stat" column since we're using the ComboBox
    dGVQuote.Columns["stat"].Visible = false;
    
    // Add the configured ComboBox column to the grid
    dGVQuote.Columns.Add(CreateComboBoxColumn());
    
    dGVQuote.Columns["valid"].Width = 135;
    dGVQuote.Columns["valid"].HeaderText = "gültig bis";
    
    // Remove the duplicate DataSource assignment here—it was resetting your column setup
}

Additional Notes

  • Ensure _status is properly populated with the id and description columns from your sdg.pa_status table (your initial SQL query looks correct for this).
  • Double-check that the values in the stat column of your main data table (from GetQuote("")) match the id values in _status—this is critical for the ComboBox to display the correct description for each row.
  • When users select a new status from the ComboBox, it will automatically update the underlying stat column with the corresponding id value from the status table, keeping your data consistent.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:00:26