Listview items

I

Islam Elkhayat

I created a listview to view 5 columns from sql database
I can search & view records using for loop but the problem is that the
object "listviewitem1" is the same so whatever record i select i get the
same first record and ignore all other records..
here is the code

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
this.listView1.Items.Clear();
this.button2.Enabled = true;
dataSet11.Clear();
string txtbox = textBox1.Text;
string sqlstr= "select EmployeeID, FirstName, LastName, Title from
employees where "+src +" like '"+txtbox+"%'";
SqlCommand searchfill = new SqlCommand(sqlstr,sqlConnection1);
sqlDataAdapter1.SelectCommand = searchfill;
sqlDataAdapter1.Fill(dataSet11);
int n= dataSet11.Tables["Employees"].Rows.Count;
statusBar1.Panels[0].Text = " Number of Employees Found :
"+n.ToString();
for(x=0; x<n; x++)
{
string str1 = dataSet11.Employees[x].FirstName.ToString();
string str2 = dataSet11.Employees[x].LastName.ToString();
string str3 =dataSet11.Employees[x].Title.ToString();
listViewItem1= new ListViewItem(new string[] {str1,str2,str3} , -1);
listView1.Items.AddRange(new ListViewItem[] {
listViewItem1});
}

}
 
A

Arne Schittenhelm

I created a listview to view 5 columns from sql database
I can search & view records using for loop but the problem is that the
object "listviewitem1" is the same so whatever record i select i get the

Hello,

I don't have plenty of time so I took a short look to your for-loop. I
checked it with a small csv-File and it works. You do not have to use
AddRange this works pretty well too:

listView1.Items.Add(listViewItem1);

So it seems that your problem is somewhere in the database query. :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top