C# 从存储过程加载DataGridView

C# 从存储过程加载DataGridView,c#,stored-procedures,datagridview,C#,Stored Procedures,Datagridview,将存储过程加载到DataGridView时遇到问题。我已经搜索了一个答案,但是我的代码看起来和我找到的每个答案都很相似。该存储过程在我添加的另一个DataGridView中运行,我将其作为固定数据源包含在其中。我是C#的新手。谁能看出我错在哪里 private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); SqlConnection myC

将存储过程加载到DataGridView时遇到问题。我已经搜索了一个答案,但是我的代码看起来和我找到的每个答案都很相似。该存储过程在我添加的另一个DataGridView中运行,我将其作为固定数据源包含在其中。我是C#的新手。谁能看出我错在哪里

    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        SqlConnection myConn = new SqlConnection("Data Source=SERVER-SQL1;Initial Catalog=OPSystem;Integrated Security=True");
        myConn.Open();
        SqlCommand myCmd = new SqlCommand("spCustomers", myConn);
        myCmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(myCmd);
        da.Fill(dt);
        dataGridView1.DataSource = da;

    }

无法绑定到SqlDataAdapter。您需要绑定到数据表

        DataTable dt = new DataTable();
        SqlConnection myConn = new SqlConnection("Data Source=SERVER-SQL1;Initial Catalog=OPSystem;Integrated Security=True");
        myConn.Open();
        SqlCommand myCmd = new SqlCommand("spCustomers", myConn);
        myCmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(myCmd);
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        dataGridView1.DataBind();
对于上下文,SqlDataAdapter

表示一组数据命令和数据库连接 用于填充数据集和更新SQL Server数据库。这个班 无法继承


无法绑定到SqlDataAdapter。您需要绑定到数据表

        DataTable dt = new DataTable();
        SqlConnection myConn = new SqlConnection("Data Source=SERVER-SQL1;Initial Catalog=OPSystem;Integrated Security=True");
        myConn.Open();
        SqlCommand myCmd = new SqlCommand("spCustomers", myConn);
        myCmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(myCmd);
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        dataGridView1.DataBind();
对于上下文,SqlDataAdapter

表示一组数据命令和数据库连接 用于填充数据集和更新SQL Server数据库。这个班 无法继承

尝试BindingSource


请尝试BindingSource

这是什么。iTManagementSystemDataSet?它似乎没有在表单的加载事件中初始化。很抱歉,它已从我的代码中删除,我已编辑了我的文章。这是什么。iTManagementSystemDataSet?它似乎没有在表单的加载事件中初始化。很抱歉,这已从我的代码中删除,我已编辑了我的帖子。非常感谢:)我没有添加行dataGridView1.DataBind();因为我认为这只适用于web表单。太好了。我不确定它是windows还是web应用程序。工作得很好。我如何管理在gridview中显示列和标题?文档会对其进行检查。非常感谢:)我没有添加dataGridView1.DataBind()行;因为我认为这只适用于web表单。太好了。我不确定它是windows还是web应用程序。工作得很好。我如何管理在gridview中显示列和标题?文档会对其进行检查。