求助:如何根据数据表为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 usingdGVQuote.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
ValueMemberandDisplayMember: 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
statcolumn since you're replacing it with the ComboBox. - Unreadable FlatStyle Assignment: Using
FlatStyle = 0is 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
_statusis properly populated with theidanddescriptioncolumns from yoursdg.pa_statustable (your initial SQL query looks correct for this). - Double-check that the values in the
statcolumn of your main data table (fromGetQuote("")) match theidvalues 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
statcolumn with the correspondingidvalue from the status table, keeping your data consistent.
内容的提问来源于stack exchange,提问作者apa13179
相关产品推荐
相关产品推荐

