Code Q & A
  • index
  • Java
  • python
  • javascript
No Result
View All Result
Code Q & A
  • index
  • Java
  • python
  • javascript
No Result
View All Result
Code Q & A
No Result
View All Result

C# combobox set custom DiplayMember

06/23/2022
in c#, combobox, winforms
Reading Time: 2 mins read

I'm binding combobox to a DataTable (table columns are "NAME", "SURNAME" and "ID"). Currently I have set ValueMember to "ID", and DisplayMember to "SURNAME". I wish to change DisplayMember - and DisplayMember only. By reading all sorts of documentation (including this forum) It can be achieved by exposing a custom property and bind combobox to that, however I'm having difficulties with It.

Closest solution was found

So I tried with this:

  private string _employee;
  public string MyDisplay
        {
            get
            {
                DataRowView r = (DataRowView)this.SelectedItem;
                DataRow s = r.Row;
                return _employee= s["SURNAME"].ToString() + " " + s["NAME"].ToString();
            }
            set
            {
                this._employee = value;
            }
        }

But that doesn't display my custom DisplayMember when I bind It to MyDisplay, when I scroll combobox Items It's Text is only System.Data.DataRowView.

What is the correct way to accomplish this ?

EDIT:

My full code for binding comboboxes:

 private void FillCombobox()
        {
            var dt = new DataTable();
            try
            {
                using (var conn = new OracleConnection(conn_string))
                {
                       using (OracleCommand cmd = new OracleCommand("MYSCHEMA.EMPLOYEES"))
                    {
                        cmd.Connection = conn;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new OracleParameter("CHOOSE_SELECT_IN", OracleDbType.Decimal, 6, ParameterDirection.Input));
                        cmd.Parameters.Add(new OracleParameter("RESULT_OUT", OracleDbType.RefCursor)).Direction = ParameterDirection.Output;

                        using (OracleDataAdapter da = new OracleDataAdapter())
                        {
                            da.SelectCommand = cmd;
                            da.Fill(dt);
                        }

                    //bind combobox
                    BindData(dt, Combobox1, "MyDisplay", "ID");
              }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void BindData(DataTable dt, ComboBox ctl, string displayMember, string valueMember)
        {
            if (ctl.InvokeRequired)
            {
                ctl.Invoke(new Action<DataTable, ComboBox, string, string>(BindData),
                               dt,
                               ctl,
                               displayMember,
                               valueMember);
            }
            else
            {
                ctl.DisplayMember = displayMember;
                ctl.ValueMember = valueMember;
                ctl.DataSource = dt;
                ctl.Text = "";
                ctl.SelectedIndex = -1;
            }
        }

Looks like there is one simple, one line mehod to do that, no property of any kind required. Just a simple add of calculated column to a Datatable in a place where you bind Combobox, and then you just set that column as DiplayMember. So my solution to all problem follows as this:

   //Add calculated column to Datatable and set this column to DiplayMember
   dt.Columns.Add("MyDisplay", typeof(string),"SURNAME + " " + NAME");

So, It's an easy one. I even included settings for display in .config, so I can change combobox DisplayMember to whatever I like. Neat solution 🙂

Tags: c#comboboxwinforms
ShareTweetPin

Related Posts

asp.net

Websocket client not connecting to websocket server

I have a NET Core 3.1 API in which I create a websocket server and start it when the API...

.net-core

Pinging Websocket server with Websocket .NET client

I'm trying to figure out how to send ping/pong heartbeats in order to be sure to maintain a connection with...

asp.net

An invalid character was found in the mail header: ';'

error on this line "message.To.Add(strCommandText);" when I try to take the email data from the database to send an email....

asp.net-core

ASP.NET Core return JSON with status code

I'm looking for the correct way to return JSON with a HTTP status code in my .NET Core Web API...

Next Post

Combobox databinding showing system.data.datarowview

Hot tags

.htaccess .net Algorithm amazon-web-services android Apache c# css c语言 django django-models docker google-app-engine google-cloud-platform hadoop Hive html ios java javascript kubernetes linux macos maven mysql node.js php postgresql python python-2-7 Python 3.X R ruby ruby-on-rails spring SQL SQL SERVER svn TensorFlow ubuntu windows xcode 爬虫 算法 编程语言

    © 2022 Websitedic.com.

    No Result
    View All Result
    • index
    • Java
    • python
    • javascript