Introduction
In this article I explain how to bind multiple column values into a ComboBox. It is very simple but I have often bound a single column in a ComboBox so it was a bit of a curiosity for me.
Use the following procedure to bind to a combo box.
Step 1
Open a Windows Forms application and insert a ComboBox and a button control on the form from the toolbox.
Step 2
Now write the simple C# code to bind the ComboBox with multiple columns.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace insert_image_in_database
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=MCNDESKTOP03;Initial Catalog=pulkit;User ID=sa;Password=wintellect@123");
cmd =new SqlCommand ("select * from emp", con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " " + ds.Tables[0].Rows[i][1] + " " + ds.Tables[0].Rows[i][2]);
}
}
}
}
Step 3
Now run your application and click on the button then see the ComboBox filled with multiple column values.