Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Populate Data from a Table in a ComboBox
By Huseyin Altindag

This article is my own approach on how to get data populated in a ComboBox. It shows also how to get connected to a MS-Access database which you can also find in the project included(sudentDB.mdb).

After having seen C#-newbies in some C#-Help-message-boards asking about the 'how to get data into ComboBox', I wanted to create a ComboBox-Programm. The Application allows you to select a StudentID and displays the columns StudentID, Student Subjet and Student Name from the table in the TextBoxes;

Using the code
After getting connected to the database 'student' in the methode 'fnGetConnectedToDatabase' (There is a small help about the database connection in comments) I assign in a foreach-loop all the columns to 3 variables(strStudentID,strStudentSubject,strStudentName) Don4t forget to create a new folder DATA on your harddisk(here D:) and copy the database 'studentDB' and 'imgCombobox.jpg' into it. The variable 'strTemp' holds all the 3 varibales with 3 stars like that 'strTemp=WS-P1998*Philosophy*Jo Marks' because I want to divide the 'strTemp' as good as in 3 different parts(words) separated by a star. I want later to get those 3 different words and display them in the TextBoxes. And then I put the content of the variable 'strTemp' into a ArrayList with a index 'i'. Now you write each strStudentID in the ComboBox 'this.comboBox1.Items.Add(strStudentID);'

private void fnGetConnectedToDatabase() 
{
...
...
foreach (DataRow myRow in myDataset.Tables["student"].Rows)
{
strStudentID =(string) myRow["studentID"];
strStudentSubject=(string) myRow["StudentSubject"]; 
strStudentName=(string) myRow["StudentName"];
strTemp=strStudentID+"*"+strStudentSubject+"*"+strStudentName;
//put strTemp into string strArraylist with int i(index for strArraylist)
this.strArraylist[i++]=strTemp;
//display in the ComboBox only strStudentID
this.comboBox1.Items.Add(strStudentID);
}
} 
As soon as you select a StudentID from the ComboBox the methode
comboBox1_SelectedIndexChanged executed
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{

string delimstr="*"; 
char [] delimeter=delimstr.ToCharArray(); 
string [] splitstr=null; 
splitstr=this.strArraylist[this.comboBox1.SelectedIndex].Split(delimeter,3); 
//Now assign the data from splitstr to TextBoxes 
this.textBox1.Text=splitstr[0]; 
this.textBox2.Text=splitstr[1]; 
this.textBox3.Text=splitstr[2]; 
} 

With 'this.strArraylist[this.comboBox1.SelectedIndex]' I get from the ArrayList
i.e 'WS-P1998*Philosophy*Jo Marks' at the position 'this.comboBox1.SelectedIndex'
and with Split(delimeter,3) I split up that into 3 words
'WS-P1998' in splitstr[0]
'Philosophy' in splitstr[1]
'Jo Marks' in splitstr[2]
Now we can assign the contents of the string 'splitstr' by index 0,1,2 
to TextBoxes textBox1, textBox2 and textBox3.
And that's it.
I hope you enjoy it! - Good coding!

Download Source