簡體   English   中英

無法將組合框中的選定值分配給SelectedIndexChanged事件中的變量。 通過組合框選擇C#Winforms填充文本框

[英]Cannot assign selected value from combobox to variable in SelectedIndexChanged event. Fill textboxes by combobox selection C# Winforms

用戶以這種形式在客戶端表中更新有關客戶端的數據。 加載表單后,它將通過組合框中的值填充文本框,從而將客戶端表中的數據填充。 當用戶更改組合框數據中的值時,int文本框必須更改。 但是,當我嘗試從組合框事件中的選定值獲取客戶端ID並將其分配給getClientID變量時,編譯器給我錯誤:

System.FormatException:輸入字符串的格式不正確。

 private void ClientUpdateForm_Load(object sender, EventArgs e)
    {
        ClientComboBox.DataSource = AgencyContext.Client.ToList();
        ClientComboBox.DisplayMember = "ClientName";
        ClientComboBox.ValueMember = "ClientID";
        Invalidate();
        int getClientID = Convert.ToInt32(ClientComboBox.SelectedValue.ToString());
        var fillTextBoxes = (from t in AgencyContext.Client where t.ClientID == getClientID select t).Single();
        ClientNametextBox.Text = fillTextBoxes.ClientName;
        ClientBirthtextBox.Text = fillTextBoxes.Birth.ToString(CultureInfo.CurrentCulture);
        ClientPhonetextBox.Text = fillTextBoxes.Phone.ToString();
        ClientPassporttextBox.Text = fillTextBoxes.Passport;
        AddresstextBox.Text = fillTextBoxes.Address;
        ClientStatetextBox.Text = fillTextBoxes.State;
        ClientCitytextBox.Text = fillTextBoxes.City;      
    }

    private void SaveNewClientButton_Click(object sender, EventArgs e)
    {

    }

    private void ClientComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
            var getClientID = Convert.ToInt32(ClientComboBox.SelectedValue.ToString());
            var fillTextBoxes = (from t in AgencyContext.Client where t.ClientID == getClientID select t).Single();
            ClientNametextBox.Text = fillTextBoxes.ClientName;
            ClientBirthtextBox.Text = fillTextBoxes.Birth.ToString(CultureInfo.CurrentCulture);
            ClientPhonetextBox.Text = fillTextBoxes.Phone.ToString();
            ClientPassporttextBox.Text = fillTextBoxes.Passport;
            AddresstextBox.Text = fillTextBoxes.Address;
            ClientStatetextBox.Text = fillTextBoxes.State;
            ClientCitytextBox.Text = fillTextBoxes.City;   
    }

在裝入事件期間...沒有選定的值...它為null,無法轉換為int。 您必須等到數據綁定之后才能拉取所選值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM