首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >DataGridViewComboBoxCell在选择后显示值成员而不显示成员

DataGridViewComboBoxCell在选择后显示值成员而不显示成员
EN

Stack Overflow用户
提问于 2018-05-31 03:12:36
回答 1查看 1.1K关注 0票数 0

因此,我一直在开发一个DataGridView,其中用户可以更改一行中一个单元格的值,然后将同一行中另一个单元格的类型更改为DataGridViewComboBoxCell或DataGridViewTextBoxCell。我可以让这个组合框像这样显示。

代码语言:javascript
复制
Dictionary<long, string> InspectionTools = new Dictionary<long, string>();

private void DynamicControlsDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (DynamicControlsDGV.Columns[e.ColumnIndex].Name == "typeDataGridViewTextBoxColumn")
    {
        var type = int.Parse(DynamicControlsDGV[1, e.RowIndex].Value.ToString());
        if (type == 8)
        {
            var CBCell = new DataGridViewComboBoxCell();
            CBCell.DataSource = InspectionTools.ToList();
            CBCell.ValueMember = "Key";
            CBCell.DisplayMember = "Value";
            CBCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            DynamicControlsDGV[7, e.RowIndex] = CBCell;
        }
        else
        {
            var CBCell = new DataGridViewTextBoxCell();
            DynamicControlsDGV[7, e.RowIndex] = CBCell;
        }
    }
}

然而,即使当我单击组合框单元格时看到所有的值,当我选择我想要的单元格时,该单元格会显示值成员(Key/ID)而不是显示成员( Value /Name)。如下图所示:

有没有一种方法可以覆盖单元格格式,这样它就不会将显示文本改回ID?

最小、完整和可验证的示例

在创建MCVE之后,我发现问题出在我的代码中的其他地方。下面是我制作的MSVE (它工作得很好)以供参考:

代码隐藏:

代码语言:javascript
复制
public partial class Form1 : Form
{
    public List<KeyValuePair<long, string>> options2 = new List<KeyValuePair<long, string>>();
    public Form1()
    {
        InitializeComponent();
        var options = new List<KeyValuePair<long, string>>();
        options.Add(new KeyValuePair<long, string>(1, "text"));
        options.Add(new KeyValuePair<long, string>(2, "combo"));

        options2 = new List<KeyValuePair<long, string>>();
        options2.Add(new KeyValuePair<long, string>(1, "option 1"));
        options2.Add(new KeyValuePair<long, string>(2, "option 2"));

        ((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).DataSource = options;
        ((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).ValueMember = "Key";
        ((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).DisplayMember = "Value";
        for (int i = 0; i < 5; i++)
        {
            DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
            dataGridView1.Rows.Add(row);
        }
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex==0)
        {
            try
            {
                if (int.Parse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString()) == 2)
                {
                    var CBCell = new DataGridViewComboBoxCell();
                    CBCell.DataSource = options2;
                    CBCell.ValueMember = "Key";
                    CBCell.DisplayMember = "Value";
                    CBCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                    dataGridView1[1, e.RowIndex] = CBCell;
                }
                else
                {
                    var CBCell = new DataGridViewTextBoxCell();
                    dataGridView1[1, e.RowIndex] = CBCell;
                }
            }
            catch
            {
            }
        }
    }
}

设计者:

代码语言:javascript
复制
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.Column2 = new System.Windows.Forms.DataGridViewComboBoxColumn();
        this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column2,
        this.Column3});
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(879, 564);
        this.dataGridView1.TabIndex = 0;
        this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
        // 
        // Column2
        // 
        this.Column2.HeaderText = "2";
        this.Column2.Name = "Column2";
        // 
        // Column3
        // 
        this.Column3.HeaderText = "3";
        this.Column3.Name = "Column3";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(879, 564);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.DataGridViewComboBoxColumn Column2;
    private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
}

另一个更新

我将字典从字典更改为字典,这是因为我的原始示例将长整型改为字符串,因为它所基于的类将string作为InstrumentName (我正在编辑的列)的类型。因此,在单元格中设置的字符串值不能与它们在字典中的长键值匹配。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 06:06:58

数据网格视图基于通过LINQ to SQL创建的类。因此,列类型被设置为string,而不是默认的Object。将组合框的数据源更改为字符串对、字符串后,单元格的格式正确。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50611927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档