c# 从零到精通 读取连接数据库-并将读取到的数据添加到DataGridViewComboBoxColumn中

c# 从零到精通 读取连接数据库-并将读取到的数据添加到DataGridViewComboBoxColumn中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test06
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
comboboxColumn.Name = “学生编号”;
this.dataGridView1.Columns.Add(comboboxColumn);
SqlConnection con = new SqlConnection(“server=.;uid=sa;pwd=;database=db_16”);
con.Open();
SqlCommand com = new SqlCommand(“select * from tb_teacher”, con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
comboboxColumn.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}
}
}

你可能感兴趣的:(数据库,c#,服务器)