How to bind ComboBox to an array of object?

Ramy Mahrous 0 Tallied Votes 2K Views Share

Here's I wrote some code to say binding not means control and data from database, it may be class holds some data

//This code developed by Ramy Mahrous 
//ramyamahrous@hotmail.com
//Its contents is provided "as is", without warranty.

/// <summary>
/// Represents Student object
/// </summary>
public class Student
{
int id;
public int ID
{
get { return id; }
set { id = value; }
}
string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string FullName
{
get { return firstName + " " + lastName; }
}
public Student(int id, string firstName, string lastName)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
}

/// <summary>
/// Binds collection of students to ComboBox
/// </summary>
public void Bind()
{
Student[] students = new Student[6];

students[0] = new Student(1, "Ramy", "Mahrous");
students[1] = new Student(2, "FCI", "Helwan");
students[2] = new Student(3, "Danny", "");
students[3] = new Student(4, "Serkan", "Sendur");
students[4] = new Student(5, "Scott", "");
students[5] = new Student(6, "adatapost", "Y");

comboBox1.Items.AddRange(students);

comboBox1.DataSource = students;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "FullName";
}
serkan sendur 821 Postaholic Banned Featured Poster

cant you use binding source for this?

Do not force your self, they are not going to make you featured coder, they are not interested in csharp. go and write some javascript if you want to get promoted :)

Ramy Mahrous 401 Postaholic Featured Poster

loooooooool :D I'm just satisfied by trying :D

qauaan 0 Newbie Poster

I cant understand the lineno 54. what is its purpose.

comboBox1.Items.AddRange(students);

Ramy Mahrous 401 Postaholic Featured Poster

@Qauaan
Just remove it, it's the same as combo.DataSource = ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.