【发布时间】:2012-07-26 08:49:33
【问题描述】:
使用 Windows 窗体和 linq to Sql,我将一个 datagridview 绑定到 Products Table,我添加到窗体 1 文本框以输入搜索到的文本。 我想知道如何根据输入的文本定位 datagridview 以查找给定的 ProductName。 这里我不想过滤行,我只想在输入每个字符后重新定位datagrid,使用的代码:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var searchValue = textBox1.Text.Trim().ToUpper();
var qry = (from p in dc.Products
where p.ProductName.ToUpper().StartsWith(searchValue)
select p).ToList();
int itemFound = productBindingSource.Find("ProductName", searchValue);
productBindingSource.Position = itemFound;
}
代码的执行给出了下一个错误:System.NotSupportedException was unhandled at the ligne:
int itemFound = productBindingSource.Find("ProductName", searchValue);
有什么想法吗?
【问题讨论】:
-
datagridview 中是否有多个项目以输入的文本开头?这可能会导致异常。此外,“查找”功能是否只查看列是否以提供的值开头?
-
在表单加载事件数据网格中填充了 Products 表的全部内容。
-
对,但是 Find 会返回多个结果吗?
-
@Chris,在出现错误消息后,datagridview 中的所有产品都没有变化,因为无法识别整数 itemFound
标签: winforms c#-4.0 linq-to-sql datagridview find