Adding data to ComboBox (unrelated data)

I want to add data to the combobox list, but I'm not sure about the correct method for this. Data comes from a raw SQL statement.

I looked at the binding data directly from the database, but it is not clear how all these bindings and datasets work for me, so I decided to skip this and insert the data into the combox myself (with your help).

The code I see on the Internet is as follows:

public partial class Form1 : Form { // Content item for the combo box private class Item { public string Name; public int Value; public Item(string name, int value) { Name = name; Value = value; } public override string ToString() { // Generates the text shown in the combo box return Name; } } public Form1() { InitializeComponent(); // Put some stuff in the combo box comboBox1.Items.Add(new Item("Blue", 1)); comboBox1.Items.Add(new Item("Red", 2)); comboBox1.Items.Add(new Item("Nobugz", 666)); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { // Display the Value property Item itm = (Item)comboBox1.SelectedItem; Console.WriteLine("{0}, {1}", itm.Name, itm.Value); } } 

Do you really need to create a new class to just add data to combobox? Also, using the technique described above, my code looks like this:

  while (rdata.Read()){ String Name = (String)rdata["vetName"]; Name = Name.Trim(); String Surname = (String)rdata["vetSurname"]; Surname = Surname.Trim(); String id = rdata["vetID"].ToString().Trim(); MessageBox.Show("ID " + id); int value1 = Convert.ToInt32(id); MessageBox.Show("value1 " + value1); String display = (String)Name + " " + Surname; editVetComboBox.Items.Add(new Item(display, 2)); } 

The problem is that while the combo box is filled with the first name and surname , the value (ID) is not added.

Any ideas?

Thank you very much Richard

+6
source share
4 answers

SOURCE: http://tipsntricksbd.blogspot.com/2007/12/combobox-is-one-of-most-common-gui.html

ComboBox is one of the most common GUI elements. It is used to provide the user with the ability to select an item from a list or enter new text. Here I will show you some common and useful functions of ComboBox in C # using Microsoft Visual Studio.Net 2005.

The simplest ComboBox:

In the simplest case, we will add some lines to the list, as shown below:

 myComboBox.Items.Add("Bangladesh"); myComboBox.Items.Add("India"); myComboBox.Items.Add("Pakistan"); myComboBox.Items.Add("Srilanka"); myComboBox.Items.Add("Maldives"); myComboBox.Items.Add("Nepal"); myComboBox.Items.Add("Bhutan"); 

Sorted List:

Typically, users expect options to be displayed in sorted order. To do this, we need to add one line of code -

 myComboBox.Sorted = true; 

DropDownStyle:

In ComboBox, the user can either enter text or simply select an item from the list. Therefore, the developer must establish his own style. 3 options are available:

 ComboBoxStyle.DropDownList: User can just select one item from a list. ComboBoxStyle.DropDown: User can either type a text or select an item from list. ComboBoxStyle.Simple: User can only type a text in text box. Item list is not shown. 

Example:

 myComboBox.DropDownStyle = ComboBoxStyle.DropDown; 

Suggesstion / Dictionary:

When a user enters text, he / she becomes happy if some prompts are displayed just below the combo box during input. For this function we need to write a couple of lines -

 myComboBox.AutoCompleteSource = AutoCompleteSource.ListItems; myComboBox.AutoCompleteMode = AutoCompleteMode.Suggest; 

Trick:

There may be times when the user selects some readable text, but for the importance of the programmer (not the selected text). For example, in a database project, StudentID is more important to the programmer than StudentName. So, it would be nice if we could add a combination (Name, Value) to the combo box and during the selection of Name we could easily get the corresponding value.

We can do this by adding an object containing the name and value.

 class ComboBoxItem { public string Name; public int Value; public ComboBoxItem(string Name, int Value) { this.Name = Name; this.Value = Value; } } myComboBox.Items.Add(new ComboBoxItem("Ashis Saha",1)); myComboBox.Items.Add(new ComboBoxItem("Subrata Roy", 2)); myComboBox.Items.Add(new ComboBoxItem("Aminul Islam", 3)); myComboBox.Items.Add(new ComboBoxItem("Shakibul Alam", 4)); myComboBox.Items.Add(new ComboBoxItem("Tanvir Ahmed", 5)); 

But if you now see the ComboBox list, you will notice that all elements are the same, and they are the class names of these objects. In fact, elements are nothing but the output of the ToString () function of these objects. Therefore, if we simply redefine the ToString () function to behave as our expectation, we are done.

 class ComboBoxItem { public string Name; public int Value; public ComboBoxItem(string Name, int Value) { this.Name = Name; this.Value = Value; } // override ToString() function public override string ToString() { return this.Name; } } 

You can get the selected value as follows:

 int selectedValue = ((ComboBoxItem)myComboBox.SelectedItem).Value; 
+8
source

No, you do not need to create a new class to add data to the ComboBox, you can just add rows if you want. Classes are for convenience, and they certainly seem convenient here - why should you avoid them? (Maybe I do not quite understand your question ...)

If all you are going to do is perform string operations and use ToString() in the class all the time, without accessing individual properties, then it makes no sense to use it - just format it in a string and use that.

+1
source

I see that this value is always 2 in your code. This may be the problem you have.

In addition, you have simplified the code a bit:

 while (rdata.Read()) { string name = (string)rdata["vetName"]; string surname = (string)rdata["vetSurname"]; int id = (int)rdata["vetID"]; string display = name.Trim() + " " + surname.Trim(); editVetComboBox.Items.Add(new Item(display, id)); } 

Assuming that the vetID is integer , and all the fields listed are not NULL in the database.

+1
source

No, you do not need to create your own class ... but it makes sense with what you do. In your application, it is recommended that you have a class type that represents your database object. Override the ToString () method of your entity class. Then paste these class types directly into the ComboBox. ComboBox will output ToString () on your entity to display the text.

Here's what it looks like:

 public class Vet { public int ID { get; set; } public string Name { get; set; } public string SurName { get; set; } public Vet() { // default constructor } public Vet( IDataRecord record ) { ID = (int) record["vetid"]; Name = (string) record["vetname"]; SurName = (string) record["vetsurname"]; } public override string ToString() { return Name + " " + SurName; } } 

Then add the instances directly to the combo box:

 comboBox.Items.Clear(); while( rdata.Read() ) { comboBox.Items.Add( new Vet( rdata ) ); } 
0
source

Source: https://habr.com/ru/post/889665/


All Articles