After setting ValueMember and DisplayMember on C#ComboBox, the selection cannot be specified from the code.

Asked 1 years ago, Updated 1 years ago, 56 views

Thank you for your help.

Paste the combo box created with the code below into the form and add the initial value of the code
I wanted to give it to you and change the selected item, but somehow it doesn't work.
The value returned by SelectedValue seems to be an object, so I wonder if that is the cause.
I think I was at a loss.

using System;
using System.Collections.General;
using System.Data;

namespace Easy
{
    public class UCombo: System.Windows.Forms.ComboBox
    {
        // Provide DataTable objects
        private DataTable tbl = new DataTable();

        public UCombo()
        {
            this.DropDownStyle=System.Windows.Forms.ComboBoxStyle.DropDownList;

            // Add columns to DataTable
            This.tbl.Columns.Add("ID", typeof(string)));
            This.tbl.Columns.Add("NAME", type of(string)));

            This.tbl.AcceptChanges();

            // Assign DataTable to Combo Box DataSource
            this.DataSource = this.tbl;

            // The displayed value is in the NAME column of the DataTable.
            This.DisplayMember="NAME";

            // The corresponding value is the ID column of the DataTable.
            This.ValueMember = "ID";

            This.clear();
        }

        public void clear()
        {
            This.tbl.Clear();
        }

        public void add (string value, string disp)
        {
            // Create a new line
            DataRow = this.tbl.NewRow();

            // Set values in each column
            row["ID"] = val;
            row["NAME"] = disp;

            // Add row to DataTable
            This.tbl.Rows.Add(row);
        }

    }
}

Retrieving a value string = SelectedValue; can be retrieved, but setting a value SelectedValue =s; where
Not reflected.

Thank you for your cooperation.

c# form

2022-09-30 15:32

1 Answers

Here After binding to DataSource, the SelectedItem appears to be created by looking at , so
This may be due to ComboBox (Forms) DataSource considerations and so on.

DisplayMemer,
when binding a data source to ComboBox.DataSource If ValueMember is not configured beforehand, the SelectedIndexChanged event must be
When I subscribe, I have a painful experience.DisplayMember, ValueMember
If you bind to DataSource before setting it up, the SelectedIndexChanged event will appear
The value of ComboBox.SelectedValue started is not the expected value (ValueMember), but
It will be the object currently selected to bind.

Follow the above to create the following parts:

// Assign DataTable to Combo Box DataSource
        this.DataSource = this.tbl;

        // The displayed value is in the NAME column of the DataTable.
        This.DisplayMember="NAME";

        // The corresponding value is the ID column of the DataTable.
        This.ValueMember = "ID";

Try changing it like this.

// The displayed value is in the NAME column of the DataTable.
        This.DisplayMember="NAME";

        // The corresponding value is the ID column of the DataTable.
        This.ValueMember = "ID";

        // Assign DataTable to Combo Box DataSource
        this.DataSource = this.tbl;

I'm sorry.After careful review, you created a ComboBox derivative class.

If you try to incorporate a derivative class into the Form within the same project as follows:
There is no need for anything strange, and I can change the SelectedValue by casting (object).
Please refer to it.

Embedded results on Form1.Designer.cs:(Form ["WindowsFormsApp1" is the project name]

private void InitializeComponent()
    {
        // ~Omitted in the middle~
        This.uCombo1 = new WindowsFormsApp1.UCombo();
        // ~Omitted in the middle~
        // 
        // uCombo1
        // 
        This.uCombo1.DisplayMember="NAME";
        this.uCombo1.DropDownStyle=System.Windows.Forms.ComboBoxStyle.DropDownList;
        This.uCombo1.FormattingEnabled=true;
        This.uCombo1.Location=new System.Drawing.Point (12,272);
        This.uCombo1.Name="uCombo1";
        This.uCombo1.Size=new System.Drawing.Size(160,20);
        This.uCombo1.TabIndex=8;
        This.uCombo1.ValueMember="ID";
        // ~Omitted in the middle~
        This.Controls.Add (this.uCombo1);
        // ~Omitted in the middle~
    }
    // ~Omitted in the middle~
    private UCombo1;

Form1.cs:( Insert data and retrieve and change selected values)

public partial class Form 1:Form
{
    // Insert data
    private void button1_Click(object sender, EventArgse)
    {
        uCombo1.add("Value01", "DisplayText01");
        uCombo1.add("Value02", "DisplayText02";
        uCombo1.add("Value03", "DisplayText03");
        uCombo1.add("Value04", "DisplayText04";
        uCombo1.add("Value05", "DisplayText05";
        uCombo1.add("Value06", "DisplayText06";
        uCombo1.add("Value07", "DisplayText07";
        uCombo1.add("Value08", "DisplayText08";
    }

    // acquisition of selected values
    private void button2_Click(object sender, EventArgse)
    {
        string str = uCombo1.SelectedValue.ToString();
        // ~Display processing of retrieved values: omitted ~
    }

    // change of selection value
    private void button3_Click(object sender, EventArgse)
    {
        uCombo1.SelectedValue=(object) "Value08";
    }
}


2022-09-30 15:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.