sorting data in tables

as abv, how can i do it im using sql server express 2005. my situation.. i had retrieved data and sort them before i add them into a combobox. so the problem now is that the ID at the combobox doesnt tally with the ID at the database. the data in the table will continue to increase as time goes..

eg.

combobox
0. apple
1.orange
2.pear

table
0.orange
1.pear
2.apple

i would like to sort the data before i can use the ID for another purpose.



Answer this question

sorting data in tables

  • stallion_alpa

    I guess u r talking abt the order it appears in the table.

    logically, it shud have an Id field, which is a primary key.

    and the listbox's value field should be bound to the Id field.

    so now it doesnt matter how you sort it, in the list.

    hope this helps



  • Eugene Ye

    SelectedIndex is just the number of the selected item in the listbox. So instead of using selectedindex, use the SelectedItem and cast this to a DataRowView and extract the value, like so:

    DataSet ds = null;
    DataTable t = null;
    private void button29_Click(object sender, EventArgs e)
    {
    ds = new DataSet("mydataset");
    t = new DataTable("mytable");
    t.Columns.Add("pkey", typeof(int));
    t.Columns.Add("display", typeof(string));
    object [] rowVal = new object[2];
    for (int i=1; i<=10; i++)
    {
    rowVal[0] = i; rowVal[1] = new string('A',5) + i;
    t.Rows.Add(rowVal);
    }
    ds.Tables.Add(t);
    listBox1.DataSource = ds.Tables["mytable"];
    listBox1.DisplayMember = "display";
    }

    private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
    DataRowView drv = (DataRowView)listBox1.SelectedItem;
    System.Diagnostics.Debug.WriteLine(drv["pkey"]);
    }



  • Masoud Farahani

    yes it has and ID column as the primary key. i have sort the retrieved data using arraylist before i add into the combobox. so now the value of the selectedindex doesnt match the ID in the table..
  • Mega Egypt

    i do not understand ur code. but somehow u gave the idea that i can use selecteditem. i will then do a check in the database with it.
  • sorting data in tables