这段代码没有给出错误,当我运行它时,它在数据网格视图中没有显示任何内容,为什么不呢?
public Form1()
{
InitializeComponent();
}
SqlConnection conexion = new SqlConnection("Server=DESKTOP-5GVNUF2;Database=Censo;Integrated Security=True");
string consulta = "SELECT * FROM InformacionEquipos";
private void Form1_Load(object sender, EventArgs e)
{
conexion.Open();
SqlDataAdapter adaptador = new SqlDataAdapter(consulta, conexion);
DataTable dt = new DataTable();
adaptador.Fill(dt);
dgvDatos.DataSource = dt;
//conexion.Close();
}
show full table in datagridview
确保
Form1_Load
正确连接到表单的 Load 事件。这可以在设计器中完成,也可以通过在 this.Load += new System.EventHandler(this.Form1_Load);
构造函数中添加 Form1
来完成。
public Form1()
{
InitializeComponent();
this.Load += new System.EventHandler(this.Form1_Load);
}