ComboBox的SelectedItem为null

时间:2014-08-07 22:44:31

标签: c# combobox

我遇到了一个特定于编程的错误,我无法解决这个错误。

这是我的代码:

try 
{
    // Get the property depending on the value in the gaugeCounter. Ex. if the value of the gaugeCounter is 2, it gets the second gauge's properties
    DataProperty currentProp = manager.Properties.GetProperty(DataPropertyClass.Table, "Gauge" + manager.Properties.GetProperty(DataPropertyClass.Table, "gaugeCounter").Value);
    // Format: column|aggregation|the word "first/last" (used as a flag)|first/last column|the word "trellis" (used as a flag)|trellis column
    tableBox.SelectedItem = currentProp.Value.ToString().Split('|')[0];
    columnBox.SelectedItem = currentProp.Value.ToString().Split('|')[1].Replace("_", " ");

    aggregation.SelectedItem = currentProp.Value.ToString().Split('|')[2];

    if (currentProp.Value.ToString().Contains("First") || currentProp.Value.ToString().Contains("Last"))
    {
        flColumn.Visible = true;
        flColumnLabel.Visible = true;    
        foreach (DataColumn tempcolumn in table.Columns)
        {
            if (tempcolumn.Properties.DataType.IsNumeric)
            {
                flColumn.Items.Add(tempcolumn);
            }

        }              

        flColumn.SelectedItem = currentProp.Value.ToString().Split('|')[3].Replace("_", " ");

        MessageBox.Show(columnBox.SelectedItem.ToString());
        MessageBox.Show(currentProp.Value.ToString().Split('|')[3].Replace("_", " "));

        if (flColumn.SelectedItem == null)
                MessageBox.Show("N");
    }
    //if (currentProp.Value.ToString().Contains("trellis"))
    //{
    //    trellisBox.SelectedItem = currentProp.Value.ToString().Split('|')[6];
    //}

    updateGauges();
}

问题似乎是flColumn.SelectedItem总是null,这会破坏我的计划。我无法弄清楚为什么,因为tableBoxcolumnBox的selectedItems都不是null,我可以确认flColumn中的项目确实存在。我打印了flColumn内的所有项目,然后返回:

萼片长度 萼片宽度 花瓣长度 花瓣宽度

我也打印出currentProp.Value.ToString().Split('|')[3].Replace("_", " "); 这给了我

花瓣宽度

但是,flColumn.SelectedItem仍然为空。 会导致这个问题的原因是什么?

由于

1 个答案:

答案 0 :(得分:0)

您可以改为测试flColumn.SelectedIndex的价值吗?

        if (flColumn.SelectedIndex == -1)
             MessageBox.Show("N");