C#使用文本框的列表视图搜索

c# Listview Search using Textbox
2020-10-17
  •  译文(汉语)
  •  原文(英语)

我有一个可以保存ID, Name,并保存BirthdayEmployee表中的ac#windows表单.我使用这些代码将数据保存在表中,使用方式为Save Button:

private void Save_Click_1(object sender, EventArgs e)
    {
        try
        {
            MainDatabaseDataSetTableAdapters.EmployeeTableAdapter employee = new MainDatabaseDataSetTableAdapters.EmployeeTableAdapter();
            employee.InsertQuery(textBox1.Text.Trim(),
                textBox2.Text.Trim(),
                textBox3.Text.Trim());


            MessageBox.Show("Saved");

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

使用此查询:

插入员工(ID,姓名,生日)值(@ ID,@ Name,@ Birthday)

我使用以下代码Populate Button显示从数据库中保存的数据Listview:

private void Populate_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

        for (int i = 0; i < dtable.Rows.Count; i++)
        {
            DataRow drow = dtable.Rows[i];

            if (drow.RowState != DataRowState.Deleted)
            {
                ListViewItem item = new ListViewItem(drow["ID"].ToString());
                item.SubItems.Add(drow["Name"].ToString());
                item.SubItems.Add(drow["Birthday"].ToString());

                listView1.Items.Add(item);
            }
        }

所以我现在的问题是我对如何listview使用textbox和进行搜索没有任何想法SearchButton.

我有一个SearchQuery:

SELECT ID,名称,生日(来自员工所在位置)
(名称= @Name)

我以为可以将其插入我的SearchButton但不知道如何.

请有人帮助.

在我使用此代码填充列表视图之前

 DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

我使用此代码进行搜索,并且有效.我使用以下代码设置数据库连接:

 public SqlCeConnection connection = new SqlCeConnection(@"Data Source = C:\...\...\...");

然后我将此代码插入我的SearchButton:

 private void button2_Click(object sender, EventArgs e)
    {


        SqlCeCommand search = new SqlCeCommand("SELECT * FROM Employee Where Name like '%" + Search.Text + "%'", connection);


        try
        {
            SqlDataReader datareader = search.ExecuteReader();


            while (datareader.Read())
            {
                ListViewItem item = new ListViewItem(datareader["ID"].ToString());
                item.SubItems.Add(datareader["Name"].ToString());
                item.SubItems.Add(datareader["Birthday"].ToString());


                listView1.Items.Add(item);
            }
        }


        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

但这不是我的老师想要的.他想看看我如何使用此代码填充Listview进行搜索

DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

我知道这里有人可能对此有一个想法.请帮我.

速聊1:
您是否尝试过组合框?stackoverflow.com/questions/11780558/…
速聊2:
你是什​​么意思@kyle
解决过程1

您还可以过滤数据行.参考

https://msdn.microsoft.com/zh-CN/library/way3dy9w(v=vs.110).aspx

像dtable.Select(Name = textbox1.text);

速聊1:
我使用了这些代码,但是我不知道该在哪里以及如何将其放入我的搜索按钮dtable.Select("名称,例如'%" + textBox4.Text +"%'");
速聊2:
当您单击搜索按钮以在文本框中写一些文本.应该是该按钮的create事件,并在该事件中编写代码.
速聊3:
您已经可以过滤查询中的数据.SqlCeCommand search =新的SqlCeCommand("SELECT * FROM Employee where name like'%" + Search.Text +"%'",connection); 不需要此代码dtable.Select("名称,例如'%" + Search.Text +"%'");
解决过程2

您可以在点击处理程序中设置dtable.DefaultView.RowFilter属性,SearchButton然后迭代方法中DataTable.DefaultView的表行而不是表行Populate_Click.

解决过程3

我只是自己解决了.感谢您浏览我的问题.这是我使用的代码

 public MainDatabaseDataSet.EmployeeDataTable getData(string data)
    {
        MainDatabaseDataSetTableAdapters.EmployeeTableAdapter returnEmployee = new MainDatabaseDataSetTableAdapters.EmployeeTableAdapter();
        return returnEmployee.GetDataByName(this.textBox4.Text.Trim());
    }

    private void Search_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        try
        {

            MainDatabaseDataSet.EmployeeDataTable GetName = getData(this.Search.Text);
            MainDatabaseDataSet.EmployeeRow GetName2 = (MainDatabaseDataSet.EmployeeRow)GetName.Rows[0];


            for (int i = 0; i < GetName.Rows.Count; i++)
            {
                DataRow drow = GetName.Rows[i];

                if (drow.RowState != DataRowState.Deleted)
                {
                    ListViewItem item = new ListViewItem(drow["ID"].ToString());
                    item.SubItems.Add(drow["Name"].ToString());
                    item.SubItems.Add(drow["Birthday"].ToString());

                    listView1.Items.Add(item);
                }


            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


    }
速聊1:
即使是这个答案,您也应该接受一个答案,因为该问题会记录为已在Stack Overflow中解决.单击绿色的勾.

I Have a c# windows form that can save ID, Name,andBirthday into my Employee Table. I use these codes to save data in my table Using Save Button:

private void Save_Click_1(object sender, EventArgs e)
    {
        try
        {
            MainDatabaseDataSetTableAdapters.EmployeeTableAdapter employee = new MainDatabaseDataSetTableAdapters.EmployeeTableAdapter();
            employee.InsertQuery(textBox1.Text.Trim(),
                textBox2.Text.Trim(),
                textBox3.Text.Trim());


            MessageBox.Show("Saved");

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

with this Query:

INSERT INTO Employee (ID, Name, Birthday) VALUES (@ID,@Name,@Birthday)

I Use Populate Button to show my saved data from my Database into Listview using this code:

private void Populate_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

        for (int i = 0; i < dtable.Rows.Count; i++)
        {
            DataRow drow = dtable.Rows[i];

            if (drow.RowState != DataRowState.Deleted)
            {
                ListViewItem item = new ListViewItem(drow["ID"].ToString());
                item.SubItems.Add(drow["Name"].ToString());
                item.SubItems.Add(drow["Birthday"].ToString());

                listView1.Items.Add(item);
            }
        }

So my Problem now is that I don't have any Idea on how to put a search in my listview using a textbox and SearchButton.

I have a SearchQuery:

SELECT ID, Name, Birthday FROM Employee WHERE
(Name = @Name)

and I thought I can insert this in my SearchButton but I don't know how.

Please somebody Help.

Before I used this code to Populate my listview

 DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

I used this codes to Search and it worked. I set my DB Connection Using this Code:

 public SqlCeConnection connection = new SqlCeConnection(@"Data Source = C:\...\...\...");

Then I inserted this codes in my SearchButton:

 private void button2_Click(object sender, EventArgs e)
    {


        SqlCeCommand search = new SqlCeCommand("SELECT * FROM Employee Where Name like '%" + Search.Text + "%'", connection);


        try
        {
            SqlDataReader datareader = search.ExecuteReader();


            while (datareader.Read())
            {
                ListViewItem item = new ListViewItem(datareader["ID"].ToString());
                item.SubItems.Add(datareader["Name"].ToString());
                item.SubItems.Add(datareader["Birthday"].ToString());


                listView1.Items.Add(item);
            }
        }


        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

but this is not what my teacher wants. He wanted to see how I do the search using this code to populate the Listview

DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

I know someone here could have an Idea on how to do this. Please help me.

Talk1:
Talk2:
what do you mean @kyle
Solutions1

You can also filter datarow. Refer

https://msdn.microsoft.com/en-us/library/way3dy9w(v=vs.110).aspx

like dtable.Select(Name=textbox1.text);

Talk1:
I used these code but I don't have any Idea where and how I will put this in my Search Button dtable.Select("Name like '%" + textBox4.Text + "%'");
Talk2:
When you click search button to write textbox some text. It should be create event of this button and write code in this event.
Talk3:
You can already filter data in query. SqlCeCommand search = new SqlCeCommand("SELECT * FROM Employee Where Name like '%" + Search.Text + "%'", connection); Do not need this code dtable.Select("Name like '%" + Search.Text + "%'");
Solutions2

You can set dtable.DefaultView.RowFilter property in SearchButton click handler and iterate DataTable.DefaultView instead of table rows in Populate_Click method.

Solutions3

I just solved it on my own. Thanks for glancing in my question. Here's the Code that I used

 public MainDatabaseDataSet.EmployeeDataTable getData(string data)
    {
        MainDatabaseDataSetTableAdapters.EmployeeTableAdapter returnEmployee = new MainDatabaseDataSetTableAdapters.EmployeeTableAdapter();
        return returnEmployee.GetDataByName(this.textBox4.Text.Trim());
    }

    private void Search_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        try
        {

            MainDatabaseDataSet.EmployeeDataTable GetName = getData(this.Search.Text);
            MainDatabaseDataSet.EmployeeRow GetName2 = (MainDatabaseDataSet.EmployeeRow)GetName.Rows[0];


            for (int i = 0; i < GetName.Rows.Count; i++)
            {
                DataRow drow = GetName.Rows[i];

                if (drow.RowState != DataRowState.Deleted)
                {
                    ListViewItem item = new ListViewItem(drow["ID"].ToString());
                    item.SubItems.Add(drow["Name"].ToString());
                    item.SubItems.Add(drow["Birthday"].ToString());

                    listView1.Items.Add(item);
                }


            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


    }
Talk1:
You should accept an answer, even if it's this one, as then the problem is recorded as solved in Stack Overflow. Click on the green tick.
转载于:https://stackoverflow.com/questions/30859202/c-sharp-listview-search-using-textbox

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入