Problem with DataView.Sort

J

jgeisler

I'm having an issue with a sort that I'm doing on a DataView to
populate a dropdown.

In my database, I have all client last names encrypted, so when I
create a dataset sorted by "LastName," the DataSet returns the records
in the order of the encrypted last names, not the actual last name.

I then put the DataSet into a DataView, and then sort the DataView,
and it *sort* of works.

The problem that I have is that the most recent record that I enter
ends up at the end of the list, no matter what the first letter is.

For example, if I add "Adams" to the database as the latest record, it
will end up as the last item in the dropdown. If I then add "Brown" to
the database, "Brown" will end up as the last item in the dropdown,
and "Adams" will go to the correct position in the sort.

I've confirmed that this happens on the Dataview sort, not when adding
it to the dropdown.

Below is the commented code:

//populate dataset existingClients
getExistingClients();

//creating datatable to populate dataview from dataset
DataSet ds = new DataSet();
DataTable dt = new DataTable("Temp");
dt.Columns.Add("ID");
dt.Columns.Add("LastName");
dt.Columns.Add("FirstName");
ds.Tables.Add(dt);
DataView dv = ds.Tables["Temp"].DefaultView;

//populate dataview
foreach (DataRow row in existingClients.Tables["Client"].Rows)
{
DataRowView drv = dv.AddNew();
// change values in the DataRow.
drv["ID"] = row["ID"].ToString();
drv["LastName"] = Decrypt(row["LastName"].ToString());
drv["FirstName"] =
row["FirstName"].ToString();
}

//sort dataview
dv.Sort = "LastName,FirstName ASC";

//add to dropdown
foreach (DataRowView row in dv)
{
ListItem item = new ListItem();
item.Text = row["LastName"].ToString() + "," +
row["FirstName"].ToString();
item.Value = row["ID"].ToString();
cboExistingClient.Items.Add(item);
}

Thank you for any help you can give.
 
M

Marc Gravell

AddNew needs to be committed, otherwise it assumes it is still being edited
(and a non-committed row always lives at the end)

Try calling EndEdit() on the row at the end of the foreach:

...
drv["FirstName"] = row["FirstName"].ToString();
drv.EndEdit();
}

Marc
 
J

jgeisler

AddNew needs to be committed, otherwise it assumes it is still being edited
(and a non-committed row always lives at the end)

Try calling EndEdit() on the row at the end of the foreach:

...
drv["FirstName"] = row["FirstName"].ToString();
drv.EndEdit();
}

Marc

Marc,

Perfect.... that did the trick. Thank you very much.

Here's the edited code for anyone else who might have the same issue:

foreach (DataRow row in existingClients.Tables["Client"].Rows)
{
DataRowView drv = dv.AddNew();
// change values in the DataRow.
drv["ID"] = row["ID"].ToString();
drv["LastName"] = Decrypt(row["LastName"].ToString());
drv["FirstName"] = row["FirstName"].ToString();
drv.EndEdit();
}
dv.Sort = "LastName,FirstName ASC";

Johnny
 

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