|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
ComboBox with two columnsHi all
I know how to fill combobox with two columns from database. But how to display only one column, when first is typeof int and second string Lets say values are 1 - Red 2 - Blue 3 - Black displayed are only Red, Blue and Black, but when user selects Blue i get back 2. BR; Mex |
|
|
Re: ComboBox with two columnsOn Mon, Oct 13, 2008 at 7:48 AM, Mex <meelis.lilbok@...> wrote:
> > Hi all > > I know how to fill combobox with two columns from database. > But how to display only one column, when first is typeof int and second > string > Lets say values are > 1 - Red > 2 - Blue > 3 - Black > > displayed are only Red, Blue and Black, but when user selects Blue i get > back 2. Create a ListStore with an int column and a string column, and use it as the combo's model. Pack a CellRendererText into the combo and map its text property to the text column in the model. You can use the GetActiveIter method to get the TreeIter of the selected row, then use listStore.GetValue (iter, column) to get the value from the store's int column. //create a list store with and int columns and a string column //and set it to be our combo's model ListStore model = new ListStore (typeof (int), typeof (string)); combo.Model = model; //use constants for our column indices so it's easier to get them right const int COL_INT = 0; const int COL_TEXT = 1; //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", COL_TEXT); //add some values to the model //note that the order of values corresponds to the columns model.AddValues (1, "Red"); model.AddValues (2, "Blue"); //get the selected value if (combo.GetActiveIter (out activeIter)) { myIntValue = model.GetValue (activeIter, COL_INT); } These same principles apply to the TreeView. -- Michael Hutchinson http://mjhutchinson.com _______________________________________________ Gtk-sharp-list maillist - Gtk-sharp-list@... http://lists.ximian.com/mailman/listinfo/gtk-sharp-list |
|
|
Re: ComboBox with two columnsHi Michael, and thnx
I have allready testet it like you described, but problem is, the int column is also still displayed.See the image; ![]() What is wrong? BR: Meelis |
|
|
Re: ComboBox with two columnsOn Tue, Oct 14, 2008 at 1:51 AM, Mex <meelis.lilbok@...> wrote:
> > Hi Michael, and thnx > > I have allready testet it like you described, but problem is, the int column > is also still displayed.See the image; > http://www.nabble.com/file/p19967446/shot.jpg > > What is wrong? Could you post the relevant parts of your code? -- Michael Hutchinson http://mjhutchinson.com _______________________________________________ Gtk-sharp-list maillist - Gtk-sharp-list@... http://lists.ximian.com/mailman/listinfo/gtk-sharp-list |
|
|
Re: ComboBox with two columnsHi Michael
hers my code ListStore ls=new ListStore(typeof(int),typeof(string)); comLibraries.Model=ls; CellRendererText cell=new CellRendererText(); comLibraries.PackStart(cell,true); comLibraries.AddAttribute(cell,"text",1); //Fill store from datareader while(dr.Read()){ ls.AppendValues(dr.GetInt32(0),dr[1].ToString()); } |
|
|
Re: ComboBox with two columnsOn Thu, Oct 16, 2008 at 6:50 AM, Mex <meelis.lilbok@...> wrote:
> > Hi Michael > > hers my code > > ListStore ls=new ListStore(typeof(int),typeof(string)); > comLibraries.Model=ls; > CellRendererText cell=new CellRendererText(); > comLibraries.PackStart(cell,true); > comLibraries.AddAttribute(cell,"text",1); > > > //Fill store from datareader > while(dr.Read()){ > ls.AppendValues(dr.GetInt32(0),dr[1].ToString()); > } Are you sure there is no other code affecting the textbox? -- Michael Hutchinson http://mjhutchinson.com _______________________________________________ Gtk-sharp-list maillist - Gtk-sharp-list@... http://lists.ximian.com/mailman/listinfo/gtk-sharp-list |
|
|
Re: ComboBox with two columnsOn Fri, Oct 24, 2008 at 7:47 PM, Michael Hutchinson
<m.j.hutchinson@...> wrote: > On Thu, Oct 16, 2008 at 6:50 AM, Mex <meelis.lilbok@...> wrote: >> >> Hi Michael >> >> hers my code >> >> ListStore ls=new ListStore(typeof(int),typeof(string)); >> comLibraries.Model=ls; >> CellRendererText cell=new CellRendererText(); >> comLibraries.PackStart(cell,true); >> comLibraries.AddAttribute(cell,"text",1); >> >> >> //Fill store from datareader >> while(dr.Read()){ >> ls.AppendValues(dr.GetInt32(0),dr[1].ToString()); >> } > > Are you sure there is no other code affecting the textbox? Er, I meant the ComboBox.. -- Michael Hutchinson http://mjhutchinson.com _______________________________________________ Gtk-sharp-list maillist - Gtk-sharp-list@... http://lists.ximian.com/mailman/listinfo/gtk-sharp-list |
|
|
Re: ComboBox with two columnsHi, 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 first 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)) Console.WriteLine(combo.Model.GetValue(iter,1)); 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 MySqlConnection(DataBase.ConnectionString); MySqlCommand Command = new MySqlCommand(SQLQuery); MySqlDataReader DataReader; try{ Connection.Open(); 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; } DataReader.Close(); } catch (MySqlException mex) { MsgBox.Show(mex.Message + Environment.NewLine + mex.Source + Environment.NewLine + mex.Number.ToString(), MessageType.Error); } catch (Exception sex) { MsgBox.Show(sex.Message + Environment.NewLine + sex.Source + Environment.NewLine, MessageType.Error); } finally { Command.Dispose(); Command.Connection.Dispose(); } } I hope this could be helpful. any doubt, comment, please mail me. |
| Free embeddable forum powered by Nabble | Forum Help |