Visual Studio 2008 C# - 我可以在 dataviewgrid 中搜索数据吗?

发布于 2024-10-25 20:13:01 字数 653 浏览 4 评论 0原文

我查遍了整个网络,仍然找不到任何有效的方法。我知道这将是一件我没有想到的简单的事情,但我已经浪费了几个小时试图解决它,但仍然一无所获。所以我的代码如下。它只是填充一个 datagridview,然后我希望用户能够通过在表单的文本框中输入联系人姓名并单击搜索按钮来进行搜索。帮助将不胜感激!

    private void frmCustomers_Load(object sender, EventArgs e)
    {

        sqlDataAdapterCustomers.Fill(dataSetNWCustomers1.Customers);

    }


    private void btnContactSearch_Click(object sender, EventArgs e)
    {
        String contactName = txtContactSearch.Text;

        if (txtContactSearch.TextLength > 0)
        {
            int r = customersBindingSource.Find("ContactName", contactName);
            customersBindingSource.IndexOf(r);

        }


    }

I've looked all over the web and still can't find anything that works. I know that it's going to be a simple thing that i've just failed to think of but i've wasted hours trying to sort it out and still nothing. So the code I have is below. It just populates a datagridview and then I want the user to be able to search by entering a Contact Name in text box on the form and click the search button. Help would be greatly appreciated!!!

    private void frmCustomers_Load(object sender, EventArgs e)
    {

        sqlDataAdapterCustomers.Fill(dataSetNWCustomers1.Customers);

    }


    private void btnContactSearch_Click(object sender, EventArgs e)
    {
        String contactName = txtContactSearch.Text;

        if (txtContactSearch.TextLength > 0)
        {
            int r = customersBindingSource.Find("ContactName", contactName);
            customersBindingSource.IndexOf(r);

        }


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-11-01 20:13:01

请使用

customersBindingSource.Filter = "ContactName ='" + contactName + "'";

如果您已将数据加载到绑定源上, 由于 DataGridView 绑定到绑定源,因此它将自行更新以仅显示搜索到的联系人。

Use

customersBindingSource.Filter = "ContactName ='" + contactName + "'";

if you already have the data loaded on the binding source. Since the DataGridView is bounded to the binding source, it will update itself to show only the contact searched.

迷路的信 2024-11-01 20:13:01

ExecuteNonQuery 用于查询 - 也就是说,它用于不返回结果集的语句。

由于您似乎正在使用强类型数据集,因此只需右键单击您的表并选择“添加”->“查询”。指定使用 SQL 语句,然后选择“返回行的 SELECT”,然后输入 SELECT * FROM Customers WHERE Con​​tactName = @ContactName 并单击“下一步”。指定一种或两种方法,并为它们提供更好的名称(FillByContactName、GetCustomerByContactName)。然后单击“完成”。

这将生成一个方法“GetCustomerByContactName(contactName)”,该方法将返回包含匹配客户(如果有)的数据表。您可以使用它来绑定到您喜欢的任何控件。

ExecuteNonQuery is for non queries - that is, it's for statements which do not return a resultset.

Since you appear to be using a strongly-typed dataset, just right-click your table and choose Add->Query. Specify to use SQL Statements, then "SELECT which returns rows", then enter SELECT * FROM Customers WHERE ContactName = @ContactName and click "Next". Specify one or both methods, and give them better names (FillByContactName, GetCustomerByContactName). Then click "Finish".

This will generate a method "GetCustomerByContactName(contactName)" which will return a DataTable with the matching customers (if any). You can use this to bind to whichever controls you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文