Vitor Oliveira
Vitor Oliveira

Reputation: 1

How search by filter a data on database by textBox to show in the listview the results in C#

I'm trying get the data defined by the text in a TextBox to show in the ListView, but i can't find the solution to that. Can someone help me??

enter image description here

enter image description here

Upvotes: 0

Views: 186

Answers (1)

Brian Daniel Tiongco
Brian Daniel Tiongco

Reputation: 61

try using this code.

IEnumerable<ListViewItem> lv = listViewItems.Items.Cast<ListViewItem>();
var matchingItems = lv.Where(i => i.Text.Contains(txtBoxName.Text));

try this..

private void populate()
    {
    listView1.Items.Clear();
    SqlCommand cm;
    if(textBox1.Text == "")
       cm = new SqlCommand("SELECT * FROM dbName", con);
    else
        cm = new SqlCommand("SELECT * FROM dbName WHERE Nome='" + txtBoxName.Text + "'", con);

    try
    {

        SqlDataReader dr = cm.ExecuteReader();
        while (dr.Read())
        {

            ListViewItem it = new 
            ListViewItem(dr["fillingcode"].ToString());
            it.SubItems.Add(dr["ID"].ToString());
            it.SubItems.Add(dr["Nome"].ToString());
            it.SubItems.Add(dr["Convenio"].ToString());
            it.SubItems.Add(dr["Contato"].ToString());
            listView1.Items.Add(it);

        }

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

}

Upvotes: 1

Related Questions