用数据集填充dataGridView

时间:2019-03-23 01:52:37

标签: c# database winforms

我正在尝试创建一个配方数据库,该数据库从用户那里获取配料并输出包含所述配料的配方。我正在尝试用sql语句的结果填充数据网格视图,但是在网格内部没有任何结果。我的SQL语句正确。这是我的代码:

resultsWindow:

        private void resultsWindow_Load(object sender, EventArgs e)
    {
        //get connection string
        string connectionString = Properties.Settings.Default.ConnectionString;

        DataSet recipeDataSet = new DataSet();
        conn = new DatabaseConnections(connectionString);

        //Get dataset
        recipeDataSet = conn.getRecipes(ingredientArray);

        //Display data in grid view
        recipesDataGrid.DataSource = recipeDataSet.Tables[0];
    }

数据库连接窗口:

        public DataSet getRecipes(string[] ingArray)
    {
        string sqlString = "SELECT recipes.Name, Instructions, recipes.Preperation_Time, Author FROM RecipeIngredients" +
                           " INNER JOIN recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID" +
                           " INNER JOIN Ingredients ON Ingredients.Ingredient_ID = RecipeIngredients.Ingredient_ID" +
                           " WHERE ingredients.Name = 'Eggs'";

        DataSet recipeDataSet = new DataSet();
        DataTable recipeDataTable = new DataTable();

        openConnection();

        dataAdapter = new SqlDataAdapter(sqlString, connectionToDB);

        //Fill dataset
        dataAdapter.Fill(recipeDataTable);
        recipeDataSet.Tables.Add(recipeDataTable);
        dataAdapter.Fill(recipeDataSet);

        closeConnection();

        return recipeDataSet;

    }

This is the data grid once I run the program

谢谢。

编辑: 我已经意识到不是我的数据表/数据集不起作用,但是我的SQL语句似乎未返回任何内容,即使将其作为单独的查询得到结果也是如此。

2 个答案:

答案 0 :(得分:2)

只需尝试..我不确定这一点

 private void resultsWindow_Load(object sender, EventArgs e)
    {
        //get connection string
        string connectionString = Properties.Settings.Default.ConnectionString;

        DataSet recipeDataSet = new DataSet();
        conn = new DatabaseConnections(connectionString);

        //Get dataset
        Datatable dt1 = conn.getRecipes(ingredientArray);

        //Display data in grid view
    recipesDataGrid.DataSource = dt1.DefaultView;


    }


   public DataTable getRecipes(string[] ingArray)
{
    string sqlString = "SELECT recipes.Name, Instructions, recipes.Preperation_Time, Author FROM RecipeIngredients" +
                       " INNER JOIN recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID" +
                       " INNER JOIN Ingredients ON Ingredients.Ingredient_ID = RecipeIngredients.Ingredient_ID" +
                       " WHERE ingredients.Name = 'Eggs'";

    DataTable recipeDataTable = new DataTable();

    openConnection();

    dataAdapter = new SqlDataAdapter(sqlString, connectionToDB);

    //Fill dataset
    dataAdapter.Fill(recipeDataTable);
    closeConnection();

    return recipeDataTable;

答案 1 :(得分:0)

这里的问题是,您已经填充了一个表,将其添加到未类型化的数据集中,然后分别填充了该数据集,这实际上应该在您的数据集中产生多个表。
要么填满表格,要么填满数据集,而不是两者都填满。我通常只填写数据集。

  

来自IDbAdapter.Fill(DataSet)的摘要:
   添加或更新System.Data.DataSet中的行以匹配数据源中的行    使用System.Data.DataSet名称,并创建一个名为    “表格”。

首先尝试将代码简化为:

public DataSet getRecipes(string[] ingArray)
{
    string sqlString = "SELECT recipes.Name, Instructions, recipes.Preperation_Time, Author FROM RecipeIngredients" +
                       " INNER JOIN recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID" +
                       " INNER JOIN Ingredients ON Ingredients.Ingredient_ID = RecipeIngredients.Ingredient_ID" +
                       " WHERE ingredients.Name = 'Eggs'";

    DataSet recipeDataSet = new DataSet();

    openConnection();

    dataAdapter = new SqlDataAdapter(sqlString, connectionToDB);

    //Fill dataset will create a table with the results
    // so you only need this one line:
    dataAdapter.Fill(recipeDataSet);

    closeConnection();

    return recipeDataSet;

}
  

您也可以删除以下行:
  dataAdapter.Fill(recipeDataSet);

要进一步验证您的逻辑,请对其进行调试并在ResultsWindow_Load中设置一个断点,在将recipeDataSet.Tables[0]设置为数据源之前检查其结果,您可以使用VS中的检查工具来执行此操作,并设置监视或使用即时控制台...

// Display data in grid view
// Inspect recipeDataSet.Tables[0], make sure there is only 1 table, and it has rows
if (recipeDataSet.Tables.Count() != 1)
    throw new ApplicationException("Expected only 1 table in the dataset");
if (recipeDataSet.Tables[0].Rows.Count() == 0)
    throw new ApplicationException("no rows found in the data table!");

recipesDataGrid.DataSource = recipeDataSet.Tables[0];

如果存在行,并且网格仍未显示和数据,请在设置数据源后调用recipesDataGrid.Refresh()强制其重新绘制,这在不使用和绑定上下文管理器时可能是必需的。 / p>

如果没有行

如果没有返回行,则首先要检查的是您的过滤条件是否正确。我将假定您已经在此处对WHERE子句进行了硬编码,并且ingArray是要过滤的成分数组。

  

请更新您的帖子,以在_load方法中包含实现ingredientArray的示例,我可以通过更完整的响应来更新此代码。

  1. 从sqlString中删除WHERE子句(现在暂时注释掉该行:

    string sqlString = "SELECT recipes.Name, Instructions, recipes.Preperation_Time, Author FROM RecipeIngredients" +
                   " INNER JOIN recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID" +
                   " INNER JOIN Ingredients ON Ingredients.Ingredient_ID = RecipeIngredients.Ingredient_ID";
    

    如果您的网格现在有数据,则说明问题在过滤条件之内。

  2. 检查您的连接字符串,请确保您要访问代码的数据库与要在其中测试的数据库相同。听起来很简单,但容易犯错。

相关问题