代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
namespace ConsoleApp3
{
class Program:Form
{
static void Main(string[] args)
{
const string name = "name";
const string id = "id";
ComboBox comboBox = new ComboBox();
DataTable dataTable = new DataTable();
DataColumn dataColumnId = new DataColumn(id, typeof(int));
DataColumn dataColumnName = new DataColumn(name, typeof(string));
dataTable.Columns.Add(dataColumnId);
dataTable.Columns.Add(dataColumnName);
dataTable.Rows.Add(1, "zhang shan");
dataTable.Rows.Add(2, "li shi");
comboBox.DataSource = dataTable;
comboBox.DisplayMember = name;
comboBox.ValueMember = id;
comboBox.Location = new System.Drawing.Point(0, 0);
Program p = new Program();
p.Controls.Add(comboBox);
Application.Run(p);
}
}
}
运行结果