# Solved: C sharp LISTVIEW - help please?



## McAvelli (Apr 19, 2008)

here how the code looks:

connection.Open();
string txtSql = "select EmployeeID, Lastname, FirstName, Title, TitleOfCourtesy, HireDate, Address, City, Homephone from Employees";

SqlCommand cmd = new SqlCommand(txtSql, connection);
SqlDataReader dr = cmd.ExecuteReader();

ListViewItem a;
while (dr.Read())
{
a = new ListViewItem();

for (int i=1; i < dr.FieldCount; i++)
{
a.Text = Convert.ToString(dr.GetValue(i));
ListViewItem.ListViewSubItem bla = new ListViewItem.ListViewSubItem(a, a.Text.ToString());
a.SubItems.Add(bla);
}

listView1.Items.Add(a);
}

dr.Close();
connection.Colse();

The output in the listview1 is: http://img140.imageshack.us/img140/3005/notcm1.png

but the first collumn should be filled from the id collumn of the db table and not phone!
What I'm missing?! Please somebody help...


----------



## pvc_ (Feb 18, 2008)

it's better if you use the listviews datasource property to populate the listview rather than using the listview. I dont code in C#, so I cant help you with details. Finding some sample source for this on msdn should be faily easy.


----------



## McAvelli (Apr 19, 2008)

pvc_ said:


> it's better if you use the listviews datasource property to populate the listview rather than using the listview. I dont code in C#, so I cant help you with details. Finding some sample source for this on msdn should be faily easy.


There's no datasource property for listview, and I can't open MSDN for some reason.
Any way, thanks!


----------



## McAvelli (Apr 19, 2008)

pvc_ said:


> it's better if you use the listviews datasource property to populate the listview rather than using the listview. I dont code in C#, so I cant help you with details. Finding some sample source for this on msdn should be faily easy.


There's no datasource property for listview, and I can't open MSDN for some reason.
Any way, thanks!


----------



## McAvelli (Apr 19, 2008)

pvc_ said:


> it's better if you use the listviews datasource property to populate the listview rather than using the listview. I dont code in C#, so I cant help you with details. Finding some sample source for this on msdn should be faily easy.


there's no datasource property for listview, and I can't open MSDN for some reason.
Anyway thanks.


----------



## McAvelli (Apr 19, 2008)

pvc_ said:


> it's better if you use the listviews datasource property to populate the listview rather than using the listview. I dont code in C#, so I cant help you with details. Finding some sample source for this on msdn should be faily easy.


there's no datasource property for listview, and I can't open MSDN for some reason.
Anyway thanks.


----------



## McAvelli (Apr 19, 2008)

there's no datasource property for listview, and I can't open MSDN for some reason.
Anyway thanks.


----------



## McAvelli (Apr 19, 2008)

there's no datasource property for listview, and I can't open MSDN for some reason.
Anyway thanks.


----------



## McAvelli (Apr 19, 2008)

there's no datasource property for listview, and I can't open MSDN for some reason.
Anyway thanks.


----------



## McAvelli (Apr 19, 2008)

there's no datasource property for listview, and I can't open MSDN for some reason.
Anyway thanks.


----------



## pvc_ (Feb 18, 2008)

you're right. Listbox and Datagrid have the datasource property:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource.aspx

I found a work around for it here:
http://www.codeproject.com/KB/list/ListView_DataBinding.aspx

I guess your method should be fine too in that case; you'd probably have to use a DataRow and a dataset to get it right.

DataRow DR =Dataset.Tables(0).Rows(0);
string ID= DR("ID");


----------



## McAvelli (Apr 19, 2008)

I solved it. Thanks *pvc_* for your help.
I just changed the query. I put EmployeeID at the end (SELECTLastname, FirstName, Title, TitleOfCourtesy, HireDate, Address, City, Homephone, *EmployeeID* from Employees).
For some reasons while adding datas to listview items, it changed the index of columns. It occured at this line of code *listView1.Items.Add(a);*. Anyway after breaking some sweat I solved it. Thanks again...


----------

