0

So I've been working on a DataGridView where the user can change the value of one cell in a row which then change the type of another cell on the same row to a DataGridViewComboBoxCell or back to a DataGridViewTextBoxCell. And I can get the combobox to show like so.

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;
        }
    }
}

However even though I see all the values when I click on the combobox cell, when I select the one I want it the cell displays the Value Member (Key/ID) and not the Display Member(Value/Name). As shown below:

What happens during run time

Is there a way I could override the cell formatting so it doesn't change the display text back to the ID?

Minimal, Complete, and Verifiable example

After making an MCVE I found that the issue is somewhere else in my code. Here is the MSVE (which works perfectly fine) that I made for reference:

Code Behind:

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
            {
            }
        }
    }
}

Designer:

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;
}

Another Update

I changed the Dictionary from a Dictionary to Dictionary which worked because my original example was changing the longs into strings because the class it was based off of had string as the type for InstrumentName (the column that I was editing). Therefore the the string values being set in the cell were not able to match their keys which were longs in the Dictionary.

Mark
  • 768
  • 3
  • 8
  • 26
  • 2
    Couldn't reproduce the problem. Consider posting [MCVE]. – Reza Aghaei May 30 '18 at 19:49
  • 1
    Do I see this right: One cell works and the other doesn't? So, looking for the differrence should be the key.. – TaW May 30 '18 at 20:19
  • The Instrument Name Column is a text column by default.I change one cell to comboboxcell (3). The combobox displays and it shows the right values when clicked on. After the value was selected it displays the ID (value member) instead of the Name(Display member) in part 4. BTW it stays as text until I click another cell so I believe something in cell formatting causes it to be formatted like the default textbox cell. – Mark May 30 '18 at 20:23
  • 1
    You were right Reza, on a smaller scale it worked. I'm going to look through the datagridview properties in case one of them may be causing this. – Mark May 30 '18 at 21:28

1 Answers1

0

The data grid view was based off of a class created via LINQ to SQL. Therefore the column type was set to string instead of the default of Object. After changing the data source for the combobox to pairs of string,string the cells became formatted correctly.

Mark
  • 768
  • 3
  • 8
  • 26