How to Fill Data with Two Display Member Based on ComboBox in C#

Submitted by: 
Language: 
Visitors have accessed this post 174 times.

In this tutorial, I will teach you how to fill data with two display member based on combobox using c#. This method will illustrate how the two fields will link each other using the concatenation process. It also has the ability to display two fields in the combobox at the same time. This method is simple yet so helpful when you are dealing with combobox.

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in C#.
ps1

Step 2

Do the form just like shown below.
ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB to access OLeDB libraries.

  1. using System.Data.OleDb;

Step 4

Create a connection between access database and c#. After that, declare all the classes that are needed.

  1. OleDbConnection con = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + Application.StartupPath + "/studentdb.accdb");
  2. OleDbCommand cmd;
  3. OleDbDataAdapter da;
  4. DataTable dt;

Step 5
Create a method for filling data in the combobox.

  1. private void fill_data(string sql,ComboBox cbo)
  2. {
  3.  
  4. try
  5. {
  6. con.Open();
  7. cmd = new OleDbCommand();
  8. da = new OleDbDataAdapter();
  9. dt = new DataTable();
  10.  
  11. cmd.Connection = con;
  12. cmd.CommandText = sql;
  13.  
  14. da.SelectCommand = cmd;
  15. da.Fill(dt);
  16.  
  17. cbo.DataSource = dt;
  18. cbo.DisplayMember = "Name";
  19. cbo.ValueMember = "ID";
  20.  
  21. }catch(Exception ex)
  22. {
  23. MessageBox.Show(ex.Message);
  24.  
  25. }
  26. finally
  27. {
  28. con.Close();
  29. da.Dispose();
  30. }
  31. }

Step 6
Do the following code to execute the method that you have created in the first load of the form.

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. string sql;
  4. sql = "SELECT ID,(Fname + ' ' + Lname) as Name FROM tblstudent";
  5. fill_data(sql, comboBox1);
  6. }

Download the complete sourcecode and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below


Comments

An alternative title for the tutorial could be... "How to fill a combo box with items using the control's DataSource, DisplayMember and ValueMember properties". or "How to create a non-value-returning function that fills a combo box with data items". Anyways, your tutorial is good.

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.