Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [C#] Problema con ComboBox

    salve!
    in un WindowsForm ho alcune ComboBox riempite da database.
    solo che io vorrei prima impostare un valore * e poi mettere tutti i valori che mi restituisce il db.
    ho provato così:
    codice:
            private void fillComboCausale()
            {
                try
                {
                    comboCausaleEntrata.Items.Insert(0, "*");
                    comboCausaleEntrata.DataSource = db.getCausale();
                    comboCausaleEntrata.ValueMember = "causale_id";
                    comboCausaleEntrata.DisplayMember = "causale_nome";
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    getCausale() è un metodo che esegue la query.
    così com'è mi vengono aggiunti tutti gli elementi dal db, ma nn il primo elemento (quello che metterei manuale).
    avete qualche idea?

  2. #2
    Prova ad inserire manualmente il valore nel DataSet che generi e non nel listitem...

  3. #3
    io io sto usando questa come funzione:
    codice:
            public DataTable getCausale()
            {
                Istanza.takeConnection().Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM causali ORDER BY causale_nome", conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dt.Dispose();
                adapter.Dispose();
                return dt;
            }
    come faccio a inserire un valore nel DataTable (o nel DataSet se pensi sia meglio)?

  4. #4
    ho provato ad aggiungere una colonna così:
    codice:
            public DataTable getCausale()
            {
                Istanza.takeConnection().Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM causali ORDER BY causale_nome", conn);
                DataTable dt = new DataTable();
                dt.Columns.Add("*");
                adapter.Fill(dt);
                dt.Dispose();
                adapter.Dispose();
                return dt;
            }
    il problema penso sia lo stesso.
    prima aggiungo un valore a mano e poi secondo me questo viene sovrascritto e nn aggiunto agli altri valori.

  5. #5
    ho anche provato a fare le cose fatte per bene:
    codice:
    namespace BancaWindowsForm
    {
        public class Causale
        {
            private int id = 0;
            private string nome = null;
    
            public Causale(int id, string nome)
            {
                this.id = id;
                this.nome = nome;
            }
    
            public int Id
            {
                get
                {
                    return id;
                }
            }
    
            public string Nome
            {
                get
                {
                    return nome;
                }
            }
    
            public override string ToString()
            {
                return nome;
            }
        }
    }
    codice:
            public List<Causale> getCausale()
            {
                List<Causale> list = new List<Causale>();
                Istanza.takeConnection().Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM causali ORDER BY causale_nome", conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                foreach (DataRow row in dt.Rows)
                {
                    list.Add(new Causale(Int32.Parse(row["causale_id"].ToString()), row["causale_nome"].ToString()));
                }
                dt.Dispose();
                adapter.Dispose();
                return list;
            }
    codice:
    comboCausaleEntrata.Items.Insert(0, "*");
    comboCausaleEntrata.DataSource = db.getCausale();
    anche così nn riesco.
    a questo punto nn so più dove sbattere la testa.
    certo è che se nn risolvo questo problema nn posso neanche continuare.

    EDIT
    codice:
            public List<Causale> getCausale()
            {
                List<Causale> list = new List<Causale>();
                Istanza.takeConnection().Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM causali ORDER BY causale_nome", conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                list.Add(new Causale(0, "*")); // QUESTA RIGA AGGIUNTA
                foreach (DataRow row in dt.Rows)
                {
                    list.Add(new Causale(Int32.Parse(row["causale_id"].ToString()), row["causale_nome"].ToString()));
                }
                dt.Dispose();
                adapter.Dispose();
                return list;
            }

  6. #6
    comboCausaleEntrata.Items.Insert(0, "*");
    comboCausaleEntrata.DataSource = db.getCausale();


    Se getCasuale ti ristituisce la nuova riga non hai più bisogno di fare comboCausaleEntrata.Items.Insert(0, "*");..


    Prova a guardare in Debug la List per vedere che ci sia effettivamente l'elemento che hai aggiunto...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.