1

我从 C# 开始,我遇到了一些问题。

红色=添加到数据库并刷新

当我在第二个窗体 (agregar_en_directorio) 中保存数据并希望在第一个窗体 (generar_tarjeta) 的组合框中显示新数据时,我想知道如何刷新数据。

Conexion:conectaraBD.cs

    public static SqlConnection ObtenerCOnexion()
    {
        SqlConnection Conn = new SqlConnection(@"Data source=MY-PC\SQLEXPRESS; Initial Catalog=myDatabase; User Id=user; Password=xxxx");
        Conn.Open();

        return Conn;
    }

组合:

    public void fillCombo()
    {
       string SQL = "select id_persona as identificador, clave_de_identificacion +' '+clave_de_la_dependencia +' '+grado_o_titulo+' '+nombre+' '+ ap+' '+ am DetallesCompletos from directorio";

        DataTable dt = new DataTable();

        using (SqlConnection Conn2 = conectaraBD.ObtenerCOnexion())
        {
            using (var cmd = new SqlCommand(SQL, Conn2))
            {

                try
                {
                    dt.Load(cmd.ExecuteReader());                        
                }
                catch (SqlException e)
                {
                    MessageBox.Show("Error al Cargar los Datos" + e.ToString(), "Error SQL",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        comboDe.DataSource = dt;
        comboDe.ValueMember = "identificador";
        comboDe.DisplayMember = "DetallesCompletos";
    }

注意:组合框的使用代码如下(用于 3 组合框的类似代码)。

并且会帮助我您对 GUI 的看法

4

1 回答 1

1

我解决了,但我不得不改变一些事情:

    public static DataTable dataFortheCombos()
    {
        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(@"Data source=SAMANIEGO-PC\SQLEXPRESS; Initial Catalog=banco_de_datos; User Id=user; Password=xxx");/

        string query = "select id_person as identifier, identification_key +' '+dependence_key +' '+degree_or_title+' '+name+' '+ ap+' '+ am as completedetails from directory"; 
        SqlCommand cmd = new SqlCommand(query, connection);

        SqlDataAdapter adap = new SqlDataAdapter(cmd);

        adap.Fill(dt);
        return dt;
    }

    public static AutoCompleteStringCollection autocompleteCombos()
    {
        DataTable dt = dataFortheCombos();

        AutoCompleteStringCollection coleccion = new AutoCompleteStringCollection();           
        foreach (DataRow row in dt.Rows)
        {
            coleccion.Add(Convert.ToString(row["completedetails"]));
        }

        return coleccion;
    }

     public void fillCombos()
    {                                
        comboFrom.DataSource = dataFortheCombos();
        comboFrom.DisplayMember = "completedetails"; //This is the value shown on the combo for the user
        comboFrom.ValueMember = "identifier"; // The selectedc value insert as identifier (is a number)
        comboFrom.SelectedIndex = -1; //Clear the combo            

        //NOTE -> The others combos (urned over and sender) using the same data

    }

事件 --onfocus-- 用于当用户位于组合框comboFrom时调用刷新

     private void comboDe_Enter(object sender, EventArgs e)
            {
                comboFrom.DataSource = null //Clear the Combo Box
                 //NOTE -> The others combos (urned over and sender) using the same data
                fillCombos();                   
            }
于 2013-06-26T03:41:54.993 回答