C# 数据未显示在DataGridView中
我已经使用VisualStudio编写了一个windows项目。我的数据条目已成功添加到我的数据库。甚至我的数据库都充满了这个数据网格中的数据 视图未显示我的数据。但是,如果我试图在数据网格视图中从数据库获取数据,它不会显示出来 我试了好几次都没用 显示数据的我的表单C# 数据未显示在DataGridView中,c#,sql-server,database,visual-studio-2019,windows-forms-designer,C#,Sql Server,Database,Visual Studio 2019,Windows Forms Designer,我已经使用VisualStudio编写了一个windows项目。我的数据条目已成功添加到我的数据库。甚至我的数据库都充满了这个数据网格中的数据 视图未显示我的数据。但是,如果我试图在数据网格视图中从数据库获取数据,它不会显示出来 我试了好几次都没用 显示数据的我的表单 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ttable.Asram;
using WinFormsApp1;
namespace Ttable
{
public partial class Demo : Form
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-OGS07R8\SQLEXPRESS;Initial Catalog=TimeTable;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter adapt;
//ID variable used in Updating and Deleting Record
int ID = 0;
public Demo()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button14_Click(object sender, EventArgs e)
{
}
private void button15_Click(object sender, EventArgs e)
{
}
private void Demo_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ShowData();
}
private void ShowData()
{
con.Open();
DataTable dt = new DataTable();
adapt = new SqlDataAdapter("select * from AddWork", con);
adapt.Fill(dt);
dataTable.DataSource = dt;
con.Close();
}
}
}
什么是数据表?它似乎不是
DataGridView
的名称-似乎您从未设置网格的数据源为什么要调用ShowData()代码>在CellContentClick事件中?将该行移动到initializeComponent()之后代码>尝试将数据源绑定到DataGridView时,不需要向其中添加任何列。因此不会触发事件CellContentClick
。您应该在InitializeComponent()之后调用ShowData()
代码>。此外,您需要设置dataGridView1.DataSource
,而不是dataTable
@KyleWang dataTable是我的dataGridView1名称@rsapøngjǣrdenlarp是我的DataGridView名称