-1

I have a code that reads the information from the database and assigns it to the list

class YaziciBilgiler
        {
            public string yaziciAdi;
         
        public List<YaziciBilgiler> YaziciSec()
            {
    
             con.Open();
            OracleCommand cmd = new OracleCommand("SELECT PRINTER_NAME FROM LABEL_PRINTER ");
                DataSet dataset = new DataSet();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
                {
                    dataAdapter.SelectCommand = cmd;
                    dataAdapter.Fill(dataset);
                }
                 
             var yazici=dataset.Tables[0].AsEnumerable().Select(datarow=>new YaziciBilgiler
                    {
                    yaziciAdi = datarow.Field<string>("PRINTER_NAME")
                }).ToList();
    
            
                con.Close(); 
                return yazici;
    }
    }
}

Then call the information in the printer list from the form

  List<YaziciBilgiler>yazici;
            yazici=yb.YaziciSec();
            foreach (var item in yazici)
            {
                cbYazici.Items.Add(item);
            }

but instead of the contents of the list, the location is as follows:

enter image description here

Hakan özel
  • 131
  • 11
  • Is it `WPF` of `WinForm`? – EgoPingvina Jul 09 '20 at 07:47
  • 1
    Take `yaziciAdi` inside `foreach`, like : `cbYazici.Items.Add(item.yaziciAdi);` – Mohammed Sajid Jul 09 '20 at 07:49
  • Does this answer your question? [Binding a WPF ComboBox to a custom list](https://stackoverflow.com/questions/561166/binding-a-wpf-combobox-to-a-custom-list) – EgoPingvina Jul 09 '20 at 07:52
  • Now you are adding the `YaziciBilgiler` items into the combobox. You should specify a property to fill the combobox, rather than an object. – 大陸北方網友 Jul 09 '20 at 07:54
  • Not sure why you're bothering to turn your datatable into a list and bind the list; a combobox can bind to a datatable! You also don't need the DataSet: an adapter can fill a datatable, nor do you need to open the connection - adapter knows how to do that too. Just put `var dt = new DataTable(); using(var adapter = new OracleDataAdapter(sqlStr, connStr)) adapter.Fill(dt);` then bind the datatable – Caius Jard Jul 09 '20 at 07:54
  • @CaiusJard how can i know about that issue? – Hakan özel Jul 09 '20 at 07:58
  • No idea; I don't really use WPF, but other people have posted links. If it was winforms it would be `combo.DataSource = dt; combo.DisplayMember = "Name of Column you want to show"; combo.ValueMember = "Name of column to use for I'd";` - WPF is probably the same 3 lines of code with the ItemsSource, DisplayMemberPath and ValueMemberPath properties – Caius Jard Jul 09 '20 at 08:08

2 Answers2

1

The basic problem with your code is that you have created a list full of objects with type YaziciBilgiler with a single string member called yaziciAdi

Combo doesn't know how to display this, it doesn't know to get the yaziciAdi property and show it, and you haven't told it so the only thing it can do when faced with an unknown object is call ToString() on it to get a string and display that string

Because you haven't overridden ToString it performs its default action which is to report the name of the class. This means your combo is full of repeated words of YaziciBilgiler

You have a few options:

  • override ToString so that it returns the yaziciAdi property
  • make your code that adds the items inside the loop add the yaziciAdi property instead of the whole object: cbYazici.Items.Add(item.yaziciAdi);
  • change the yaziciAdi member to be a property instead. bind the combobox to the list, or to the datatable- the discussion for this is in the comments to the question. I don't use WPF but reading around, I think the code might be something like:
   cbYazici.SetBinding(
   ItemsControl.ItemsSourceProperty, 
   new Binding { Source = yazici });
    cbYazici.DisplayMemberPath = "yaziciAdi";

Or use an equivalent in the XAML (if the codebehind can't set these; I've no idea whether it can or not, and any experienced WPF guys feel free to edit this answer if I've got this wrong)

Note: to bind property yaziciAdi MUST be a property, not a field

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
1

You can try this :

cbYazici.Items.Add(item.yaziciAdi)