[Gtk-sharp-list] Combobox clear and reload

Michael Hutchinson m.j.hutchinson at gmail.com
Fri Oct 10 18:38:34 EDT 2008


On Thu, Oct 9, 2008 at 12:58 PM, henkdp <henk at bbd.co.za> wrote:
>
> Good day,
>
> I am a total newbie to Monodevelop Stetic and GTK#. :blush: I have searched
> high and low on the internet and this forum, don't know if I am looking for
> wrong key words but here is my problem.
>
> I want to create a ComboBoxEntry widget with employees in it, I populate it
> with values from the database, nothing fancy and I managed that. However,
> when a new employee is added I want to clear the ComboBoxEntry and recreate
> it, I don't just want to append the new value at the bottom.
>
> Here, to explain is a snippet of my code loading the ComboboxEntry:
>
>   protected void LoadEmployeeCombo()
>   {
>      //Load the employee details
>
>      DataTable datatableEmployeeDropDown =
> business.StartUpLoadEmployeeDetails();
>      foreach  (DataRow  r in datatableEmployeeDropDown.Rows )
>      {
>         string empnumber = r["empnumber"].ToString();
>
>         string nickname = r["nickname"].ToString();
>         string trim_nickname = nickname.Trim();
>
>         string surname = r["surname"].ToString();
>         string trim_surname = surname.Trim();
>
>         string employeeNumberAndName = ( empnumber + " - " + trim_nickname
> + " " + trim_surname );
>
>         HTEmpNocomboboxentry.AppendText( employeeNumberAndName );
>      }
>
>      //Add this to the top of the combo entry box for when a new employee
> is needed
>      HTEmpNocomboboxentry.PrependText("New - type new employee number " );
>
>   }
>
> As I said nothing fancy and you can see from the code I am a real newbie!

You may need to delve into the more advanced aspects of the
TreeView/TreeModel system.

//create a list store with one column, of type string, and set it to
be our combo's model
ListStore model = new ListStore (typeof (string));
combo.Model = model;

//create a text renderer and add it to the combo box
//note that you can pack in multiple renderers, e.g. an icon renderer
CellRendererText textRenderer = new CellRendererText ();
combo.PackStart (textRenderer, true);

//map the "text" property of the renderer to column 0 in the model
//note that you can map any columns in the model to any properties of
the renderer
combo.AddAttribute (textRenderer, "text", 0);

//add some values to the model
model.AddValues ("foo");

//clear the model
model.Clear ();

These same principles apply to the TreeView.

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Gtk-sharp-list mailing list