Listview order {RESOLVED}

Listview with SQL Data

Page 1 of 1

4 Replies - 1403 Views - Last Post: 05 July 2009 - 10:22 AM Rate Topic: -----

#1 b.ihde   User is offline

  • D.I.C for life
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Listview order {RESOLVED}

Posted 05 July 2009 - 09:26 AM

Hi!

I m filling my listview with SQL data.
Here my code:

SqlConnection conn = new SqlConnection();
			conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + connection1.ToString() + ";Integrated Security=True;Connect Timeout=30;User Instance=True;";
			conn.Open();
			string comand = "SELECT * FROM Changes";
			DataTable dt = new DataTable();
			SqlDataAdapter adapter = new SqlDataAdapter(comand, conn);
			adapter.Fill(dt);
			conn.Close();
			listView1.View = View.Details;
			listView1.Columns.Add("Datum/Uhrzeit", 150);
			listView1.Columns.Add("User", 100);
			listView1.Columns.Add("Aktion", 100);
			listView1.Columns.Add("Objekt", 100);
			listView1.Columns.Add("Objektstamm", 100);

			for (int i = 0; i < dt.Rows.Count; i++)
			{
				ListViewItem lvi = new ListViewItem(dt.Rows[i]["Date"].ToString());
				ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem(lvi, dt.Rows[i]["Username"].ToString());
				ListViewItem.ListViewSubItem lvsi2 = new ListViewItem.ListViewSubItem(lvi, dt.Rows[i]["ChangeType"].ToString());
				ListViewItem.ListViewSubItem lvsi3 = new ListViewItem.ListViewSubItem(lvi, dt.Rows[i]["ChangeElement"].ToString());
				ListViewItem.ListViewSubItem lvsi4 = new ListViewItem.ListViewSubItem(lvi, dt.Rows[i]["ChangeRoot"].ToString());

				lvi.SubItems.Add(lvsi);
				lvi.SubItems.Add(lvsi2);
				lvi.SubItems.Add(lvsi3);
				lvi.SubItems.Add(lvsi4);

				listView1.Items.Add(lvi);
				


This works fine, but my list is not in a correct order... Per example if my ListViewItem is "ID" instead of "Date" it would look like:

ID
1
2
3
4
7
5
6

Sometimes one ID is even added twice?

Any ideas how this could happen?

Thanks!

Ben

This post has been edited by b.ihde: 05 July 2009 - 10:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Listview order {RESOLVED}

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Listview order {RESOLVED}

Posted 05 July 2009 - 09:57 AM

Try changing your SQL query to this

string comand = "SELECT DISTINCT Date, Username, CHangeType, CHangeElement, ChangeRoot FROM Changes ORDER BY Date ASC";



For starters using SELECT * is bad practice, always specify what columns you want. Second the above query will sort the results based on the date. Hope taht helps :)
Was This Post Helpful? 1
  • +
  • -

#3 b.ihde   User is offline

  • D.I.C for life
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Listview order {RESOLVED}

Posted 05 July 2009 - 10:05 AM

Ahh yes of course!

I almost forgot to think about to use this ORDER BY statement..

Thankfully you found my post ;)

Thanks again PsychoCoder!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Listview order {RESOLVED}

Posted 05 July 2009 - 10:19 AM

No problem, glad I could help.

Now go forth and become an ORDER BY ninja
Was This Post Helpful? 0
  • +
  • -

#5 b.ihde   User is offline

  • D.I.C for life
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Listview order {RESOLVED}

Posted 05 July 2009 - 10:22 AM

View PostPsychoCoder, on 5 Jul, 2009 - 05:19 PM, said:

No problem, glad I could help.

Now go forth and become an ORDER BY ninja



haha, sure i ll do!

I didnīt even know that SELECT * is bad practice..
I have a german book about Databases and their statements.. and again i have to say:
german books are just bad copys of the original english books..

Anyway, know i learned again something about working with SQL ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1