[Gtk-sharp-list] ComboBox with two columns

Madian madian001 at gmail.com
Tue Sep 8 12:14:10 EDT 2009

Hi, everybody,
well at first let me say, that we must to understand how the combobox are
made ,
we have a list, and an entry(in ComboBoxEntry), a model,
To get data from some database or another kind of dataprovider you need at
to define what column or data will be displayed in the dropdown list
for example if have a simple table let's say Countries

Table Countries
Id Int
Name varchar(25)

I assume you wish to show Name column, ok?
so the TreeModel for your combo look's like this

TreeModel ComboData = new TreeModel(typeof (string), typeof (int));

Always, and i don't now why, but always the first column must to be a
string, the rest 
of data types it doesn't mather.

Please se my sample down.
After, when you try to get the id, or value asosiated to the 
item selected in the combobox 

if(combo.GetActiveIter(out iter))

This is a simple Sub, that I made for to fill a combo with data
To do something like the DataSource property it does in the WindowsForm

public static void SetDataSource (string SQLQuery,
                                  Gtk.ComboBoxEntry Combo)
//Here the origin of data are set
	MySqlConnection	Connection	= new
	MySqlCommand	Command	= new MySqlCommand(SQLQuery);
	MySqlDataReader	DataReader;
		Command.Connection = Connection;
		DataReader = Command.ExecuteReader();
		if (DataReader.HasRows)
		{//Declare a simple liststore
			ListStore store = new ListStore(typeof (string),typeof(int));
                        //Fill the liststore
			while (DataReader.Read())
				store.AppendValues (DataReader.GetString(1),DataReader.GetInt32(0));
                        //Set this liststore as model of your combo
			Combo.Model = store;
                        //this part it's optional if you wish to enable
search into the combo
			Combo.Entry.Completion = new EntryCompletion();
			Combo.Entry.Completion.Model = store;
			Combo.Entry.Completion.TextColumn = 0;
	catch (MySqlException  mex)
		MsgBox.Show(mex.Message +
		            Environment.NewLine +
		            mex.Source +
		            Environment.NewLine +
	catch (Exception sex)
		MsgBox.Show(sex.Message +
		            Environment.NewLine +
		            sex.Source +

I hope this could be helpful. any doubt, comment, please mail me.
View this message in context: http://www.nabble.com/ComboBox-with-two-columns-tp19953369p25349318.html
Sent from the Mono - Gtk# mailing list archive at Nabble.com.

More information about the Gtk-sharp-list mailing list