Reputation: 1
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??
Upvotes: 0
Views: 186
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