【问题标题】:Cannot assign selected value from combobox to variable in SelectedIndexChanged event. Fill textboxes by combobox selection C# Winforms无法将组合框中的选定值分配给 SelectedIndexChanged 事件中的变量。通过组合框选择填充文本框 C# Winforms
【发布时间】:2015-04-13 14:07:47
【问题描述】:

在此表单中,用户更新客户端表中有关客户端的数据。加载表单时,它会按组合框中的值填充来自客户端表的数据的文本框。当用户更改组合框数据 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;   
    }

【问题讨论】:

  • ClientNamesComboBox.SelectedValue.ToString() 的值是多少,你的CurrentCulture 是多少?在该行设置断点,调试您的代码并告诉我们。
  • 值包含来自客户端表的客户端 ID。它应该是 int 类型。
  • 不,不是类型。当您调试代码并用鼠标悬停SelectedValue 属性时,它就是
  • 你是说这个{System.Data.Entity.DynamicProxies.Client_554ADCCF96234938E3C159149B78911E3A5B9F49DE47E343082953245E9D34EA}?
  • 很可能 ClientNamesComboBox.SelectedValue 是空字符串结果

标签: c# winforms entity-framework combobox


【解决方案1】:

在加载事件期间......没有选定的值......它是空的,不能转换为int。您必须等到数据绑定之后才能提取所选值。

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多