How To Fill A Combobox Using A DataTable In C#
In This C# Tutorial We Will See How To Populate A ComboBox With DataTable Data In Form Load Using CSharp Programming Language And Visual Studio Editor.
Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class PopulateComboboxFromDatatable : Form
{
public PopulateComboboxFromDatatable()
{
InitializeComponent();
}
private void PopulateComboboxFromDatatable_Load(object sender, EventArgs e)
{
// create a datatable
DataTable table = new DataTable();
// add columns to datatable
table.Columns.Add("id");
table.Columns.Add("username");
// add rows to datatable
table.Rows.Add("1","PHP");
table.Rows.Add("2", "C#");
table.Rows.Add("3", "Java");
table.Rows.Add("4", "Javascript");
table.Rows.Add("5", "C++");
comboBox1.DataSource = table;
comboBox1.DisplayMember = "username";
}
}
}
///////////////OUTPUT:
Download Projects Source Code