winforms - 如何通过代码将 BindingSource 移动到特定记录

标签 winforms c#-4.0 linq-to-sql datagridview bindingsource

使用绑定(bind)到绑定(bind)源控件的 datagridview 绑定(bind)到 LINQ to SQL类,我想知道如何将 bindingSource 定位到特定记录,也就是说,当我在文本框中键入产品名称时,bindingsource 应该移动到该特定产品。这是我的代码:

在我的表格 FrmFind 中:

    NorthwindDataContext dc;
    private void FrmFind_Load(object sender, EventArgs e)
    {
        dc = new NorthwindDataContext();

        var qry = (from p in dc.Products
                   select p).ToList();

        FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);

        productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;

        int index = productBindingSource.Find("ProductName", tb.Text);

        if (index >= 0)
        {
            productBindingSource.Position = index;
        }
    }

在程序类中:
    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))
                if (property.GetValue(item).ToString().StartsWith(key.ToString()))
                {
                    return i;
                }
            }
            return -1; // Not found
        }
    }

如何实现 find 方法以使其工作?

最佳答案

您可以结合 BindingSource.Find()使用 Position 的方法属性(property)。

例如,如果您的 TextBox changed 事件处理程序中有类似的内容:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    int index = bs.Find("Product", tb.Text);

    if (index >= 0)
    {
        bs.Position = index;
    }
}

这当然取决于很多事情,例如绑定(bind)源的数据源所具有的 Find 方法的特定实现。

在你刚才问的一个问题中,我给了你一个 Find 的实现,它适用于完全匹配。下面是一个稍微不同的实现,它将查看被检查属性的开始:
protected override int FindCore(PropertyDescriptor property, object key)
{
    // Simple iteration:
    for (int i = 0; i < Count; i++)
    {
        T item = this[i];
        if (property.GetValue(item).ToString().StartsWith(key.ToString()))
        {
            return i;
        }
    }
    return -1; // Not found
}

请注意,上述方法区分大小写 - 如果需要,您可以将 StartsWith 更改为不区分大小写。

关于 .Net 的工作方式需要注意的一个关键点是对象的实际类型总是不够的——声明的类型是消费代码所知道的。

这就是您获得 NotSupported 的原因调用 Find 方法时出现异常,即使您的 BindingList实现有一个 Find 方法 - 接收此绑定(bind)列表的代码不知道 Find。

原因在于这些代码行:
dc = new NorthwindDataContext();

var qry = (from p in dc.Products
           select p).ToList();

FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);

productBindingSource.DataSource = list.OrderBy(o => o.ProductName);

当您为绑定(bind)源设置数据源时,您包括扩展方法 OrderBy - 检查这表明它返回 IOrderedEnumerable,一个描述的接口(interface) here在 MSDN 上。注意这个接口(interface)没有 Find 方法,所以即使底层的 FindableBindingList<T>支持查找绑定(bind)源不知道。

有几种解决方案(我认为最好的方法是扩展您的 FindableBindingList 以支持排序和排序列表),但对于您当前的代码,最快的方法是像这样提前排序:
dc = new NorthwindDataContext();

var qry = (from p in dc.Products
           select p).OrderBy(p => p.ProductName).ToList();

FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);

productBindingSource.DataSource = list;

在 WinForms 中,对于您正在尝试做的事情,没有完全开箱即用的解决方案 - 它们都需要一些自定义代码,您需要将这些代码组合在一起以满足您自己的需求。

关于winforms - 如何通过代码将 BindingSource 移动到特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11714069/

相关文章:

c# - Winform,鼠标点击弹出窗口

c++ - 如何在 Visual Studio 2010 中构建这个简单的 C++/SWIG/C# 项目?

c# - 如何允许方法接受字符串或整数?

linq-to-sql - Linq to SQL 扩展性方法定义

c# - Winforms 中的简单相机捕获

c# - Winforms:从另一个类更新表单上的标签

c# - 如何在多个线程之间进行通信?

mysql - 使用 LINQ 获取具有多对多关系的子表的所有记录

c# - 封装LINQ select语句

c# - 在 asp.net/C# 中使用 LinqToSql 对 row_number 进行排序