如何将datagridview定位到搜索的文本输入 [英] How do I position a datagridview to the searched text input

查看:198
本文介绍了如何将datagridview定位到搜索的文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Windows窗体和linq到Sql,我将一个datagridview绑定到Products表,我添加到1 Textbox来输入搜索的文本。
我不知道如何根据输入的文本来定位datagridview以找到给定的ProductName。
这里我不想过滤行,我只想在每个字符输入后重新定位datagrid,使用的代码:

Using Windows forms and linq to Sql, I bound a datagridview to Products Table, I added to the form 1 Textbox to input the searched text. I wonder how to position the datagridview according to the text entered to find a given ProductName. Here I do not want to filter rows, I only want to reposition datagrid after each character entered, the used code:

    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未处理ligne:

The execution of code give the next error: System.NotSupportedException was unhandled at the ligne:

int itemFound = productBindingSource.Find("ProductName", searchValue);

有任何想法吗?

推荐答案

MSDN文档 BindingSource 有以下答案:

The MSDN documentation for BindingSource has the answer:


Find方法只能在底层列表为
实现了带有搜索的IBindingList。这个方法简单地将
请求引用到底层列表的IBindingList.Find方法。对于
示例,如果底层数据源是DataSet,DataTable或
DataView,此方法将propertyName转换为PropertyDescriptor
并调用IBindingList.Find方法。 Find的行为,例如
,如果没有找到匹配项,则返回的值取决于底层列表中方法的
实现。

The Find method can only be used when the underlying list is an IBindingList with searching implemented. This method simply refers the request to the underlying list's IBindingList.Find method. For example, if the underlying data source is a DataSet, DataTable, or DataView, this method converts propertyName to a PropertyDescriptor and calls the IBindingList.Find method. The behavior of Find, such as the value returned if no matching item is found, depends on the implementation of the method in the underlying list.

当你在一个BindingSource上调用这个方法时,它的底层数据源不会实现IBindingList,你会看到这个异常(由 IBindingList.FindCore

When you call this method on a BindingSource whose underlying data source does not implement IBindingList then you see the exception (thrown by the default implementation of IBindingList.FindCore:


.NotSupportedException:不支持指定的方法。

System.NotSupportedException: The specified method is not supported.

您不显示绑定绑定源, t实现此方法。

You don't show what you bind the binding source to but clearly it doesn't implement this method.

很奇怪, BindingList< T> 推荐的列表类型为您的数据源使用不提供 FindCore 实现。

Annoyingly, BindingList<T> the recommended list type to use for your data source does not provide a FindCore implementation.

如果您使用BindingList,创建自己的自定义类型下面是一个支持find的BindingList的绝对裸框实现的代码:

If you are using BindingList you will need to create your own custom type. Here is the code for an absolutely bare bones implementation of a BindingList that supports find:

public class FindableBindingList<T> : BindingList<T>
{

    public FindableBindingList()
        : base()
    {
    }

    public FindableBindingList(List<T> list)
        : base(list)
    {
    }

    protected override int FindCore(PropertyDescriptor property, object key)
    {
        for (int i = 0; i < Count; i++)
        {
            T item = this[i];
            if (property.GetValue(item).Equals(key))
            {
                return i;
            }
        }
        return -1; // Not found

    }
}

很多与你自己的实现BindingList如支持排序。我已经离开我的答案只是最小支持find方法。

You can do lots with your own implementations of BindingList such as supporting sorting. I've left my answer as just the minimum to support the find method. Search for SortableBindingList if you want to know more.

要使用此类,请执行以下操作:

To use this class do something like this:

var qry = (from p in dc.Products
               where p.ProductName.ToUpper().StartsWith(searchValue)
               select p).ToList();    

FindableBindingList<YourType> list = new FindableBindingList<YourType>(qry);

dataGridView1.DataSource = list;

这篇关于如何将datagridview定位到搜索的文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆