从使用字典填充的组合框中读取值成员

read value member from combobox populated using dictionary

我在 form_load 事件中使用 dictionary 填充了 combobox,其值和显示成员如下

Dictionary<string, int> GravelCount = new Dictionary<string, int>
{
    {1,"text value 1"},
    {2,"text value 2"},
};

CB_GravelCount.DataSource = new BindingSource(GravelCount, null);
CB_GravelCount.DisplayMember = "Key";
CB_GravelCount.ValueMember = "Value";
CB_GravelCount.SelectedIndex = -1;

此值成员存储在 sql 服务器数据库中...一切正常,直到我尝试读取此存储的数据并在 [=14] 事件时重新分配给 combobox =] 数据不会被分配组合框值,因为它得到索引 -1 没有错误,所以什么都不显示

public int GetPersonification()
{
    string connStr = ConfigurationManager.ConnectionStrings["SRJDconnstr"].ToString();
    string cmdStr = @"SELECT ID,
                                             SIZ,
                                             PLACE,
                                             ONE_OR_MORE,
                                             R_OR_L,
                                             EKO_OR_ASH,
                                             NOTICE
                                     FROM PERSONIFICATION
                                   WHERE SEANCE_ID=@SEANCE_ID;";

    using (SqlConnection conn = new SqlConnection(connStr))
    using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
    {

            conn.Open();
            cmd.CommandText = cmdStr;
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add(new SqlParameter("@SEANCE_ID", SqlDbType.Int)).Value = F0102.vSessionID;

            SqlDataReader sqlReader = cmd.ExecuteReader();

            while (sqlReader.Read())
            {
                PersonificationID = Convert.ToInt32(sqlReader[0].ToString());
                TB_Size.Text = sqlReader[1].ToString();                              //SIZ
                CB_TreatmentPlace.SelectedValue = sqlReader[2].ToString(); //PLACE
                CB_GravelCount.SelectedValue = sqlReader[3].ToString();      //ONE_OR_MORE
                CB_Side.SelectedValue = sqlReader[4].ToString();                 //R_OR_L
                CB_TreatmentWay.SelectedValue = sqlReader[5].ToString(); //EKO_OR_ASH
                TB_Note.Text = sqlReader[6].ToString();                             //NOTICE
            }
            return 1;
    }
}

您很可能在读取数据库时出错。在代码的 SqlDataReader 部分周围放置一个 try/catch 以查看到底是什么失败了。我的猜测是,您的其中一列数据有空值,这会导致 ToString 失败或 PersonificationId 的 Convert.ToInt32 失败。阅读返回的异常消息很可能会让您走上正确的方向。 试试这个:

public int GetPersonification()
{
  string connStr = ConfigurationManager.ConnectionStrings["SRJDconnstr"].ToString();
  string cmdStr = @"SELECT ID,
                          SIZ,
                        PLACE,
                  ONE_OR_MORE,
                        R_OR_L,
                    EKO_OR_ASH,
                        NOTICE
          FROM PERSONIFICATION
          WHERE SEANCE_ID=@SEANCE_ID;";

  using (SqlConnection conn = new SqlConnection(connStr))
  using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
  {
    //Add the try here
    try
    {
      conn.Open();
      cmd.CommandText = cmdStr;
      cmd.CommandType = CommandType.Text;

      cmd.Parameters.Add(new SqlParameter("@SEANCE_ID", SqlDbType.Int)).Value = F0102.vSessionID;

      SqlDataReader sqlReader = cmd.ExecuteReader();
      int? persId = null;
      string siz,
      string place = "";
      string one_or_more = "";
      string r_or_l = "";
      string eko_or_ash = "";
      string notice = "";
      while (sqlReader.Read())
      {
          persId = sqlReader["ID"] as int? ?? default(int);
          siz = sqlReader["SIZ"] as string;
          place = sqlReader["PLACE"] as string;
          one_or_more = sqlReader["ONE_OR_MORE"] as string;
          r_or_l = sqlReader["R_OR_L"] as string;
          eko_or_ash = sqlReader["EKO_OR_ASH"] as string;
          notice = sqlReader["NOTICE"] as string;
      }
      PersonificationID = persId ?? 0; //null coalesce
      TB_Size.Text = siz;
      CB_TreatmentPlace.SelectedValue = place;
      CB_GravelCount.SelectedValue = one_or_more;
      CB_Side.SelectedValue = r_or_l;
      CB_TreatmentWay.SelectedValue = eko_or_ash;
      TB_Note.Text = notice;
      return 1;
    }
    catch (Exception ex)
    {
      //Read the exception message using your debugger.
      return 0;
    }
  }
}

解析为整数。

CB_GravelCount.SelectedValue = int.Parse(sqlReader[3].ToString());