【问题标题】:ComboBox is not accepting Null for a selected Item [closed]ComboBox 不接受所选项目的 Null [关闭]
【发布时间】:2020-08-10 12:14:17
【问题描述】:

我收到以下异常:

System.FormatException: 输入字符串格式不正确

我收到此异常消息是因为我有一个 Selection Change Event 填充 ComboboxesTextBoxes 中的数据来自 DataGrid 中的选定行

我的ComboBox 显示从DataGrid 行中选择的项目,当单击下拉列表时,它会显示剩余的Selected Item List

如果从DataGrid 中选择的行是NULL,那么我会收到此异常消息,因为我的ComboBox 无法将Null 识别为其列表中的项目之一。

这是我的Selection Change Event 代码,我在cmb_DisputeClassification.SelectedValue = row_selected["FR_DSP_CLSF"].ToString(); 线上遇到了这个异常

        private void dtGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        // When a respective row is selected
        // Populate the key dispute information in botton left panel. 
        DataGrid gd = (DataGrid)sender;
        DataRowView row_selected = gd.SelectedItem as DataRowView;

        if (row_selected != null)
        {
            // Customer Information Menu
            txt_acct.Text = row_selected["ACCOUNT"].ToString();
            txt_custname.Text = row_selected["CUST_NAME"].ToString();
            txt_address.Text = row_selected["PREM_ADDR"].ToString();
            txt_revclass.Text = row_selected["RateType"].ToString();
            txt_DisplayID.Text = row_selected["DSP_ID"].ToString();



            // Dispute Information Menu - Review Section

            // Review Notes Textbox
            txt_ReviewNotes.Text = row_selected["FR_CMMNT"].ToString();
            txt_ResolutionNotes.Text = row_selected["COMMENT"].ToString();
            DatePicker_ScheduledFor.Text = row_selected["FR_SO_DT_WNTD"].ToString();
            txt_WFMissuedBy.Text = row_selected["NM_USER"].ToString();


            // First Review Parse is needed for ShortDate format.
            if (row_selected["FR_DT_FIRSTREV"] != null)
            {
                DateTime dateTime;
                if (DateTime.TryParse(row_selected["FR_DT_FIRSTREV"].ToString(), out dateTime))
                {
                    txt_firstreview.Text = dateTime.ToShortDateString();
                }
                else
                {
                    txt_firstreview.Text = ""; //assign default value
                }
            }

            // First Review Parse is needed for ShortDate format.
            if (row_selected["FR_TS_LATESTUPD"] != null)
            {
                DateTime dateTime;
                if (DateTime.TryParse(row_selected["FR_TS_LATESTUPD"].ToString(), out dateTime))
                {
                    txt_Latestupdate.Text = dateTime.ToShortDateString();
                }
                else
                {
                    txt_Latestupdate.Text = ""; //assign default value
                }
            }

            // Comboboxes 

            SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");

            try
            {
                // Dispute Classification ComboBox
                SqlDataAdapter Status_data0 = new SqlDataAdapter("SELECT * FROM [hb_DspClsf]", connection);
                DataSet ds0 = new DataSet();
                Status_data0.Fill(ds0, "t");

                cmb_DisputeClassification.ItemsSource = ds0.Tables["t"].DefaultView;
                cmb_DisputeClassification.DisplayMemberPath = "TXT_DSP_CLSF";
                cmb_DisputeClassification.SelectedValuePath = "KY_DSP_CLSF";
                cmb_DisputeClassification.SelectedValue = row_selected["FR_DSP_CLSF"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
    }

【问题讨论】:

  • 那么row_selected["FR_DSP_CLSF"] 在运行时返回了什么?您是否调试过您的代码以找出答案?
  • @mm8 是的,我做到了,它返回的SelectedValueNull,它返回的SelectedValuePath""
  • 我没有问SelectedValue 返回什么,是吗?什么是“FR_DSP_CLSF”?您认为从阅读您的问题中可以清楚地看到这一点吗?这不是……
  • 它返回的值范围是 1-14。然后它显示数据库中我的hb_DspClsf 表中的Selected Value Path。该表名为TXT_DSP_CLSF

标签: sql sql-server wpf datagrid


【解决方案1】:

错误不是由 Combobox 引起的,而是由同一行上的 ToString() 调用引起的,您不能在 NULL 引用上调用。

string myString = null
myString; //no error
myString.ToString(); //error

但是,使用Convert.ToString() 可以避免这个问题。

string myString = null
Convert.ToString(myString) //no error

【讨论】:

  • 成功了,谢谢!
猜你喜欢
  • 2016-10-02
  • 1970-01-01
  • 2020-10-18
  • 2018-02-01
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 2010-11-07
  • 2010-11-30
相关资源
最近更新 更多