C# 为什么我的DataGridView数据源不绘制行?
我的DataGridView.DataSource有问题,解决这个问题花费了我很多时间。代码如下:C# 为什么我的DataGridView数据源不绘制行?,c#,linq,datagridview,ienumerable,datasource,C#,Linq,Datagridview,Ienumerable,Datasource,我的DataGridView.DataSource有问题,解决这个问题花费了我很多时间。代码如下: string[] queryWords = singleQuery.Split(' '); // Split the query words according the "space" character // Compose the SQL select query string selectClause = @"SELECT ID, CategoryID, Name, U
string[] queryWords = singleQuery.Split(' '); // Split the query words according the "space" character
// Compose the SQL select query
string selectClause = @"SELECT ID, CategoryID, Name, UnitID, Price, QuantityAgen, QuantityBanjer, QuantityMalalayang, QuantitySorong FROM Product WHERE ";
// Add the where clauses inside the SQL select query
for (int i = 0; i < queryWords.Length; i++)
{
selectClause += "(Name LIKE '%" + queryWords[i] + "%')";
if (i < queryWords.Length - 1)
selectClause += " AND ";
}
// Initiate the query and get the appropriate results
IEnumerable<SumberRejekiProgram.Code.Product> resultQuery = dbProduct.ExecuteQuery<SumberRejekiProgram.Code.Product>(selectClause);
var finalResult = from p in resultQuery
select new { Name = p.Name, Price = p.Price, Unit = p.Unit.Name };
// Bind the DataGridView according to the final query result in resultQuery variable
dgvSearch.DataSource = resultQuery;
string[]queryWords=singleQuery.Split(“”);//根据“空格”字符拆分查询词
//组合SQL选择查询
string selectClause=@“从产品中选择ID、类别ID、名称、单位ID、价格、数量代理、数量Banjer、数量Malalayang、数量Sorong”;
//在SQL select查询中添加where子句
for(int i=0;i
调试代码时,“resultQuery”和“finalResult”都包含我想要的结果。但是,当我设置“dgvSearch.DataSource”时,即使我同时尝试了dgvSearch.DataSource=resultQuery
和dgvSearch.DataSource=finalResult
,结果也不会显示在行中。DataGridView只是空的(列除外)
我在代码执行后调试“dgvSearch”,以确保数据源正常工作,并且它确实正常工作。所有结果都在DataSource内部,但不知何故DataGridView不会显示它,尽管我调用了dgvSearch.show()
有人能帮我吗?我觉得我想自杀。
提前非常感谢。您应该在设置数据源后尝试调用Page.DataBind方法 您还可以尝试使用bindingsource,如下所述:
在设置其
DataSource
属性后,是否调用dgvSearch.DataBind()
另外,在SQL中直接使用字符串连接是非常非常糟糕的(快速搜索“SQL注入”)。要么将查询参数化,要么将其应用到Linq查询中。根据您使用的特定提供商,这可能有效,也可能无效:
// Initiate the query and get the appropriate results
var resultQuery = from p in dbProduct.Product
select new { Name = p.Name, Price = p.Price, Unit = p.Unit.Name };
// Add the where clauses
for (int i = 0; i < queryWords.Length; i++)
{
resultQuery = resultQuery.Where(p => p.Name.Contains(queryWords[i]));
}
// Bind the DataGridView according to the final query result in resultQuery variable
dgvSearch.DataSource = resultQuery;
dgvSearch.DataBind();
//启动查询并获得适当的结果
var resultQuery=来自dbProduct.Product中的p
选择新{Name=p.Name,Price=p.Price,Unit=p.Unit.Name};
//添加where子句
for(int i=0;ip.Name.Contains(queryWords[i]);
}
//根据resultQuery变量中的最终查询结果绑定DataGridView
dgvSearch.DataSource=resultQuery;
dgvSearch.DataBind();
问题在于,您已将LINQ查询设置为运行,但尚未实际执行它,即使您尝试将其结果绑定到DataGridView时也是如此。要执行LINQ查询,您需要“触摸”查询结果,例如在for
循环中或通过调用ToList()
。这被称为“延迟执行”
因此,更改绑定到该行的行,它应该可以工作(假设您的代码在其他方面是正确的):
如果你不感谢某人,为什么你认为他应该帮助你?没有dgvSearch.DataBind();与2013年相比
dgvSearch.DataSource = resultQuery.ToList();