I am adding a Customer using stored procedures. I somehow managed to select newly added customer after insertion, but I cannot focus on the record in listbox..
I tried everything, like :
lbxCustomers.ScrollIntoView(this.lbxCustomers.SelectedIndex);
and many modifications using Items and stuff, but nothing is working. It's still not scrolling the view to selected item.. Any ideas?
It's WPF.
My init looks like this :
private void init()
{
SqlConnection connection = null;
SqlDataReader reader = null;
try
{
connection = new SqlConnection(this.strConnection);
connection.Open();
SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers", connection);
SqlDataAdapter sda = new SqlDataAdapter(command);
reader = command.ExecuteReader();
ListItem listItem;
while (reader.Read())
{
listItem = new ListItem(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
this.lbxCustomers.Items.Add(listItem);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
}
finally
{
connection.Close();
reader.Close();
}
}
init2 is the same except the line :
SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE CompanyName = '" + correctID + "'", connection);
It reffers to ListItem which looks like this :
class ListItem
{
private string customerID, companyName, contactName;
public ListItem(string customerID, string companyName, string contactName)
{
this.customerID = customerID.Replace("'", "''");
this.companyName = companyName.Replace("'", "''");
this.contactName = contactName.Replace("'", "''");
}
public override string ToString()
{
return this.companyName + " (" + this.contactName + ")";
}
public string CustomerID
{
get { return this.customerID; }
}
}
Change your code to:
ListItem listItem = null;
while (reader.Read())
{
listItem = new ListItem(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
this.lbxCustomers.Items.Add(listItem);,
lbxCustomers.SelectedItem = listItem;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.