使用数据集填充 dataGridView



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

结果窗口:

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;
}

这是我运行程序后的数据网格

提前谢谢。

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

试试..我不确定这个

private void resultsWindow_Load(object sender, EventArgs e)
{
//gets connection string
string connectionString = Properties.Settings.Default.ConnectionString;
DataSet recipeDataSet = new DataSet();
conn = new DatabaseConnections(connectionString);
//Gets dataset
Datatable dt1 = conn.getRecipes(ingredientArray);
//Displays 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);
//Fills dataset
dataAdapter.Fill(recipeDataTable);
closeConnection();
return recipeDataTable;

这里的问题是,您已经填充了一个表,将其添加到非类型化数据集中,然后单独填充了数据集,这实际上应该会导致数据集中有多个表。
要么填充表,要么填充数据集,而不是两者兼而有之。我通常只是填充数据集。

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

首先尝试将代码简化为以下内容:

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()强制其重新绘制,这在不使用和绑定上下文管理器时可能是必要的。

如果没有行

如果没有返回任何行,首先要检查的是筛选条件是否正确。我假设您在此处对 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. 检查连接字符串,确保代码正在访问的数据库与要测试的数据库相同。听起来很简单,但很容易犯错误。

最新更新