0

ストアドプロシージャを使用して顧客を追加しています。挿入後、なんとか新規追加のお客様を選択できましたが、リストボックスのレコードに集中できません。

私は次のようにすべてを試しました:

lbxCustomers.ScrollIntoView(this.lbxCustomers.SelectedIndex);

アイテムなどを使用した多くの変更がありますが、何も機能していません。まだビューを選択したアイテムにスクロールしていません。何かアイデアはありますか?

WPFです。

私の初期化は次のようになります:

        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は、次の行を除いて同じです。

SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE CompanyName = '" + correctID + "'", connection);

これは、次のようなListItemを参照します。

    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; }
    }

}
4

1 に答える 1

0

コードを次のように変更します。

        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;
        }
于 2013-01-08T10:12:52.743 に答える