Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据未显示在DataGridView中_C#_Sql Server_Database_Visual Studio 2019_Windows Forms Designer - Fatal编程技术网

C# 数据未显示在DataGridView中

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.

我已经使用VisualStudio编写了一个windows项目。我的数据条目已成功添加到我的数据库。甚至我的数据库都充满了这个数据网格中的数据

视图未显示我的数据。但是,如果我试图在数据网格视图中从数据库获取数据,它不会显示出来

我试了好几次都没用

显示数据的我的表单

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名称