简体   繁体   English

ComboBox SelectedItem属性不会更改

[英]ComboBox SelectedItem property won't change

Hi I've got a problem with a comboBox. 嗨,我有一个comboBox问题。
At the beginning the documentTypeComboBox.SelectedItem value is null, then the GetDataRow() method return DataRow which is not null (100% sure that it return not null value for my test) 首先, documentTypeComboBox.SelectedItem值为null,然后GetDataRow()方法返回不为null的DataRow(100%确保它对于我的测试返回的不是null值)
After all the value of the SelectedItem property is still null. 毕竟,SelectedItem属性的值仍为null。 No idea what is wrong. 不知道怎么了。 I'm 100% sure that the returned DataRow is assigned as comboBox DataSource row 我100%确定将返回的DataRow分配为comboBox DataSource行

    public void SetDefaultData()
    {
        documentTypeComboBox.SelectedItem = null;

        dataWystawieniaDate.Value = _Faktura.DataWystawienia;
        dataSprzedazyDate.Value = _Faktura.DataSprzedazy;

        warehouseComboBox.SelectedItem = Helpers.GetDataRow(_Dictionary.Magazines, _Faktura.Magnum);
        paymentMethodComboBox.SelectedItem = Helpers.GetDataRow(_Dictionary.SpPaymentMethodsData, _Faktura.IdSposobuPlatnosci);
        priceComboBox.SelectedItem = Helpers.GetDataRow(_Dictionary.PricesData, _Faktura.RodzajCeny);
        discountComboBox.SelectedItem = Helpers.GetDataRow(_Dictionary.RabatyData, _Faktura.RodzajRabatu);
        documentTypeComboBox.SelectedItem = Helpers.GetDataRow(_Dictionary.SpDocumentsCodes, _Faktura.Kod, documentTypeComboBox.DisplayMember);

        documentNumberTextBox.Text = _Faktura.Numer;
        kwotaBruttoTextBox.Text = _Faktura.PlnWartoscBrutto.ToString();
    }


    public static DataRow GetDataRow(DataTable dt, string value, string fieldName)
    {
        foreach (DataRow dr in dt.Rows)
        {
            if (dr[fieldName].ToString() == value)
                return dr;
        }
        return null;
    }

I think i figure it out. 我想我知道了。 SelectedItem property is DataRowView not DataRow. SelectedItem属性是DataRowView而不是DataRow。 This explains why it doesn't select the correct value. 这解释了为什么它没有选择正确的值。 Use SelectedValue instead :) 改用SelectedValue :)

https://social.msdn.microsoft.com/Forums/windows/en-us/77b23eb9-112f-4fea-9d3a-486b822f22ea/combobox-datatable-selecteditem https://social.msdn.microsoft.com/Forums/windows/zh-cn/77b23eb9-112f-4fea-9d3a-486b822f22ea/combobox-datatable-selecteditem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM