[Gtk-sharp-list] Combobox with values from databases

Harold Alfonso Reina Herrera harold_reina75 at hotmail.com
Fri Jun 20 15:53:12 UTC 2014






Esto tan solo es un ejemplo extraído de un programa que tengo en desarrollo. yo use Nhibernate y gtk3


public class Country {
	public virtual string Cod { get; set; }	public virtual string Name { get; set; }	public virtual string DialPrefix { get; set; }	public virtual IList<State> States { get; set; }
	public Country() {		States = new List<State>();	}}
public class State {
	public virtual string Cod { get; set; }	public virtual Country Contry { get; set; }	public virtual string Name { get; set; }	public virtual string DialZone { get; set; }	public virtual IList<City> Cities { get; set; }
	public State() {		Cities = new List<City>();	}            }
public class City : DomainObject<uint> {
	public virtual State State { get; set; }	public virtual string Name { get; set; }	public virtual string AreaCode { get; set; }}

public partial class Dialog : Gtk.Windows {
	private Gtk.ComboBox ComboCountry, ComboState, ComboCity;	private Gtk.ListStore StoreCountry, StoreState, StoreCity;
	public Dialog () : base ("Ejemplo") {		var Grid0 = new Gtk.Grid () {			Visible = true,			CanFocus = false,			RowSpacing = 6,			ColumnSpacing = 6,			BorderWidth = 2		};		this.StoreCountry = new Gtk.ListStore (typeof(Country));		this.StoreState = new Gtk.ListStore (typeof(State));		this.StoreCity = new Gtk.ListStore (typeof(City));

		this.ComboCountry = new Gtk.ComboBox { Visible = true,CanFocus = false,};		CellRendererText = new Gtk.CellRendererText {			Height = 22,			SingleParagraphMode = true,			WrapWidth = 250,		};		this.ComboCountry.PackStart (CellRendererText, false);		this.ComboCountry.SetCellDataFunc (CellRendererText, new Gtk.CellLayoutDataFunc (OnGetCountry));		Grid0.Attach (this.ComboCountry, 0, 0, 1, 1);		this.ComboState = new Gtk.ComboBox {			Visible = true,			CanFocus = false,		};		CellRendererText = new Gtk.CellRendererText {			Height = 22,			SingleParagraphMode = true,			WrapWidth = 250,		};		this.ComboState.PackStart (CellRendererText, false);		this.ComboState.SetCellDataFunc (CellRendererText, new Gtk.CellLayoutDataFunc (OnGetState));		Grid0.Attach (this.ComboState, 0, 1, 1, 1);		this.ComboCity = new Gtk.ComboBox {			Visible = true,			CanFocus = false,		};		CellRendererText = new Gtk.CellRendererText {			Height = 22,			SingleParagraphMode = true,			WrapWidth = 250,		};		this.ComboCity.PackStart (CellRendererText, false);		this.ComboCity.SetCellDataFunc (CellRendererText, new Gtk.CellLayoutDataFunc (OnGetCity));		Grid0.Attach (this.ComboCity, 0, 2, 1, 1);						this.ContentArea.PackStart (Grid0, false, false, 6);		ShowAll ();						this.ComboState.Changed += delegate {			Gtk.TreeIter iter;			if (this.ComboState.GetActiveIter (out iter)) {				var state = ((Sicpifc.Dao.Domain.State)this.StoreState.GetValue (iter, 0));				this.StoreCity.Clear ();				foreach (var item in state.Cities)					this.StoreCity.AppendValues (item);					this.ComboCity.Active = 0;					QueueDraw ();				}			};		this.ComboCountry.Changed += delegate {			Gtk.TreeIter iter;			if (this.ComboCountry.GetActiveIter (out iter)) {				var countries = ((Sicpifc.Dao.Domain.Country)this.StoreCountry.GetValue (iter, 0));				this.StoreState.Clear ();				foreach (var item in countries.States)					this.StoreState.AppendValues (item);				this.ComboState.Active = 0;				QueueDraw ();			}		};		GetDatos ();
    }            internal void GetDatos () {    	//Nhibernate    	var items = this.Session.QueryOver<Country> ().List ();    	foreach (var item in items)			this.StoreCountry.AppendValues (item);		this.ComboCountry.Active = 0;    }            protected void OnGetCountry (Gtk.ICellLayout cell_layout, Gtk.CellRenderer cell, Gtk.ITreeModel tree_model, Gtk.TreeIter iter) {		var item = (Country)tree_model.GetValue (iter, 0);		if (item != null)			(cell as Gtk.CellRendererText).Text = item.Name;	}
	protected void OnGetState (Gtk.ICellLayout cell_layout, Gtk.CellRenderer cell, Gtk.ITreeModel tree_model, Gtk.TreeIter iter) {		var item = (State)tree_model.GetValue (iter, 0);		if (item != null)			(cell as Gtk.CellRendererText).Text = item.Name;	}
	protected void OnGetCity (Gtk.ICellLayout cell_layout, Gtk.CellRenderer cell, Gtk.ITreeModel tree_model, Gtk.TreeIter iter) {		var item = (City)tree_model.GetValue (iter, 0);		if (item != null)			(cell as Gtk.CellRendererText).Text = item.Name;	}			internal void SetCity (City city) {			if (city == null)				return;			SetContry (city.State.Contry.Cod);			SetState (city.State.Cod);
			Gtk.TreeIter iter;			if (this.ComboCity.Model.GetIterFirst (out iter)) {				do {					var item = (City)this.ComboCity.Model.GetValue (iter, 0);					if (item.Id == city.Id) {						this.ComboCity.SetActiveIter (iter);						break;					}				} while (this.ComboCity.Model.IterNext (ref iter));			}		}
		internal void SetContry (string codigo) {			Gtk.TreeIter iter;			if (this.ComboCountry.Model.GetIterFirst (out iter)) {				do {					var item = (Country)this.ComboCountry.Model.GetValue (iter, 0);					if (item.Cod == codigo) {						this.ComboCountry.SetActiveIter (iter);						break;					}				} while (this.ComboCountry.Model.IterNext (ref iter));			}		}
		internal void SetState (string codigo) {			Gtk.TreeIter iter;			if (this.ComboState.Model.GetIterFirst (out iter)) {				do {					var item = (Sicpifc.Dao.Domain.State)this.ComboState.Model.GetValue (iter, 0);					if (item.Cod == codigo) {						this.ComboState.SetActiveIter (iter);						break;					}				} while (this.ComboState.Model.IterNext (ref iter));			}		}
}



> Date: Fri, 20 Jun 2014 03:02:20 -0700
> From: hugaomrodrigues at gmail.com
> To: gtk-sharp-list at lists.ximian.com
> Subject: [Gtk-sharp-list] Combobox with values from databases
> 
> Hi,
> 
> I have a little question. When I used Visual Studio I fill the combo box
> using a Dataset with values from a database. Like this:
> 
> DataSet ds = class_geral.mysql_dataset(sql, "s");
> comboBox_tipo_conta.DataSource = ds.Tables["s"];
> comboBox_tipo_conta.DisplayMember = "type_account";
> comboBox_tipo_conta.ValueMember = "type_account"; 
> 
> But in Mono Develop, with Gtk# the options of DataSource, DisplayMember and
> ValueMember aren't available. Can anyone tell me how I fill the combo box
> with data from MariaDB?
> 
> Thanks in advance,
> Hugo Rodrigues.
> 
> 
> 
> --
> View this message in context: http://mono.1490590.n4.nabble.com/Combobox-with-values-from-databases-tp4663160.html
> Sent from the Mono - Gtk# mailing list archive at Nabble.com.
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
 		 	   		   		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20140620/cfdfdbad/attachment-0001.html>


More information about the Gtk-sharp-list mailing list