代码之家  ›  专栏  ›  技术社区  ›  sam

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

  •  0
  • sam  · 技术社区  · 7 年前

    我已经居住了 combobox dictionary 关于 form_load

    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 server数据库中。。。在我尝试读取存储的数据并重新分配到之前,一切正常 下拉列表框 形式荷载 没有分配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;
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Ercan Peker    7 年前

    解析为int。

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

        2
  •  0
  •   Mr. Cooper    7 年前

    试试这个:

    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;
        }
      }
    }