简体   繁体   English

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

[英]Visual Studio 2008 C# - Can I search for data in dataviewgrid?

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. 它只是填充一个datagridview,然后我希望用户能够通过在表单上的文本框中输入联系人姓名并单击搜索按钮来进行搜索。 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);

        }


    }

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. 由于DataGridView已绑定到绑定源,因此它将更新自身以仅显示搜索到的联系人。

ExecuteNonQuery is for non queries - that is, it's for statements which do not return a resultset. ExecuteNonQuery用于查询-即,用于不返回结果集的语句。

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". 指定使用SQL语句,然后选择“选择返回行”,然后输入SELECT * FROM Customers WHERE ContactName = @ContactName并单击“ Next”。 Specify one or both methods, and give them better names (FillByContactName, GetCustomerByContactName). 指定一种或两种方法,并给它们更好的名称(FillByContactName,GetCustomerByContactName)。 Then click "Finish". 然后点击“完成”。

This will generate a method "GetCustomerByContactName(contactName)" which will return a DataTable with the matching customers (if any). 这将生成方法“ GetCustomerByContactName(contactName)”,该方法将返回具有匹配客户(如果有)的DataTable。 You can use this to bind to whichever controls you like. 您可以使用它绑定到您喜欢的任何控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataViewGrid同步C# - DataViewGrid synchronization C# C#,Visual Studio 2008 - C#, Visual Studio 2008 Visual Studio 2008和C# - Visual Studio 2008 and C# C# | Forms .NET | 如何制作 ComboBox 以显示 Excel 工作表,然后将其显示在 dataViewGrid 中? - C# | Forms .NET | How can I make a ComboBox that show's Excel sheets which it then displays in the dataViewGrid? 我可以在Visual Studio 2008中从C#项目构建2个或更多dll吗? - Can I build 2 or more dlls from C# project in Visual Studio 2008? 无法使用C#visual Studio 2008将数据插入表中 - Cannot insert the data into the table using C# visual studio 2008 我可以在Visual Studio 2008中使用C#的代码编辑器显示一行分隔方法吗? - Can I have the Code Editor for C# in Visual Studio 2008 show a line separating methods? 在 Visual Studio 2008 中单步执行 C# 时如何找到方法调用者? - How can I find a method caller when stepping through C# in Visual Studio 2008? 为什么在Visual C#2008 Studio中找不到Var关键字? - Why can't I find the Var keyword in my Visual C# 2008 Studio? 有没有办法像C#一样从Visual Studio 2008的VB中的类中提取接口代码? - Is there a way I can extract an interface code from a class in VB on visual studio 2008 like C# does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM