I am setting items in a combobox with this:
Dictionary<int, string> myItems = await resp.Content.ReadAsAsync<Dictionary<int, string>>();
myCmb.Items.Clear();
myCmb.ItemsSource = myItems;
myCmb.SelectedValuePath = "Key";
myCmb.DisplayMemberPath = "Value";
Then I want to select item by Key (for example, select item with Key=5) in another part of the code. How can this be done?
myCmb.SelectedItem = myItems.FirstOrDefault(n => n.Key == desiredKey);
You can do it this way but you will need to have myItems accessible from the another part of code.
Related
I am trying to write a custom object and have to retrieve one custom related field from custom object Course Master(master) to detail object Training deals. This code is showing error
List<List<String>> strList = new List<List<String>>();
List<Training_deal__c> td = [select name, Course_master__r.course__c from Training_deal__c];
for(Training_deal__c t : td){
List<String> tempList = new List<String>();
tempList.add('Training Deals');
tempList.add(t.name);
tempList.add(t.course__c);
strList.add(tempList);
}
Try This
tempList.add(t.Course_master__r.course__c);
I tried to like this and it is working properly
List<List<String>> strList = new List<List<String>>();
List<Training_deal__c> td = [select name, Course_master__r.course__c from Training_deal__c];
for(Training_deal__c t : td){
List<String> tempList = new List<String>();
tempList.add('Training Deals');
tempList.add(t.name);
tempList.add(t.Course_master__r.course__c);
strList.add(tempList);
}
I have a function that creates a new value inside an associative array.
var array:Array = new Array(new Array());
var i : int = 0;
function personVelgLand(evt:MouseEvent)
{
for(i = 0; i < personListe.dataProvider.length; i++)
{
if(txtPersonVelg.text == personListe.dataProvider.getItemAt(i).label)
{
personListe.dataProvider.getItemAt(i).reise = array;
personListe.dataProvider.getItemAt(i).reise.push(landListe.selectedItem.land);
}
}
}
What happens is that the 'array' array which becomes 'personListe.dataProvider.getItemAt(i).reise' applies to every item in the list. I want it so that each time the function runs that the .reise only applies to the item chosen and not all of them.
EDIT:
I did this:
personListe.dataProvider.getItemAt(i).reise = new Array(array);
And now they are not the same but now each item in the list can not have multiple .reise values...
EDIT 2: dataProvider is nothing it would work just as fine without it. .reise is created in the function I originally posted it creates .reise in the object getItemAt(i).
personListe is a list which the users add their own items to by the use of a input textfield. It is given by this function:
function regPerson(evt:MouseEvent)
{
regPersoner.push(txtRegPerson.text);
personListe.addItem({label:regPersoner});
regPersoner = new Array();
txtRegPerson.text = "";
}
EDIT 3 : The user can register names which turn in to labels in a list. There is also list with 3 countries, Spain, Portugal and England. The user can then register a country to a person they select. Thats when I want to create the .reise inside the "name" items in the first list which contains the countries they have selected. I want every name to be able to select multiple countries which then will be created in the element .reise inside the item that holds their name. This would be easy if I could use strings. But later I plan to have the user type in a country and then something that would show every user that have selected that country. That is why the countries need to be stored as arrays inside the "name" items..
You should first create a class for the User data that you are modelling. You already know all the properties.
The user can register names
The user can then register a country to a person they select.
able to select multiple countries
Such a class could look like this:
package
{
public class User
{
private var _name:String;
private var _countries:Array;
public function User(name:String)
{
_name = name;
_countries = [];
}
public function get name():String
{
return _name;
}
public function get countries():Array
{
return _countries;
}
public function set countries(value:Array):void
{
_countries = value;
}
}
}
Now create a DataProvider, fill it with objects of that class and use it for the list as described here:
import fl.controls.List;
import fl.data.DataProvider;
var users:List = new List();
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
addChild(users);
users.move(150, 150);
In order to get a label from a User object, define a labelFunction
import fl.controls.List;
import fl.data.DataProvider;
var users:List = new List();
users.labelFunction = userLabelFunction;
function userLabelFunction(item:Object):String
{
return item.name;
}
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
addChild(users);
users.move(150,150);
This way you do not have to add a label property that you don't want in your class.
Selecting a name means selecting a user. The list of countries associated to the name should show up in a second List.
The DataProvider of that List remains constant, a list of all the available countries.
import fl.controls.List;
import fl.data.DataProvider;
// list of users
var users:List = new List();
addChild(users);
users.move(150,150);
users.labelFunction = userLabelFunction;
function userLabelFunction(item:Object):String
{
return item.name;
}
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
// lsit of countries
var countries:List = new List();
addChild(countries);
countries.move(550,150); // adjut position as desired
countries.dataProvider = new DataProvider([
{label:"a"},
{label:"b"},
{label:"c"},
{label:"d"},
{label:"e"},
{label:"f"}]);
Now all you have to do is to wire it all up. If a user is selected, select his countries in the countries list. If a country is selected, add that to the currently selected users list of countries. That could look somethign like this:
users.addEventLsitener(Event.CHANGE, onUserSelected);
function onUserSelected(e:Event):void
{
countries.selectedItems = users.selectedItem.countries;
}
countries.addEventLsitener(Event.CHANGE, onCountrySelected);
function onCountrySelected(e:Event):void
{
users.selectedItem.countries = countries.selectedItems;
}
The full code could look like this. I did not test this, but you get the idea.
// list of users
var users:List = new List();
addChild(users);
users.move(150,150);
users.labelFunction = userLabelFunction;
function userLabelFunction(item:Object):String
{
return item.name;
}
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
// list of countries
var countries:List = new List();
addChild(countries);
countries.move(550,150); // adjut position as desired
countries.dataProvider = new DataProvider([
{label:"a"},
{label:"b"},
{label:"c"},
{label:"d"},
{label:"e"},
{label:"f"}]);
// events
users.addEventLsitener(Event.CHANGE, onUserSelected);
function onUserSelected(e:Event):void
{
countries.selectedItems = users.selectedItem.countries;
}
countries.addEventLsitener(Event.CHANGE, onCountrySelected);
function onCountrySelected(e:Event):void
{
users.selectedItem.countries = countries.selectedItems;
}
From what I understand this seems to work except for the fact that the names are already provided when the program starts. What I want is that the user adds the name themselves while the program is running.
You can add new items with the methods provided by the DataProvider class, like addItem() for example. Just add new User objects.
I'm trying to select a node in TreeListLookupEdit.
var fn = treeListLookupEdit1.FindNodeByKeyID(NodeId);
treeListLookupEdit1.Properties.TreeList.FocusedNode = fn;
My TreeListLookupEdit is already filled with the data (from an EF datasource), I need to focus the desired row and see this value in both treeListLookUpEdit1.Text (when it is in a closed state) and when I open a popup window too.
But nothing happens, it does not selects the node.
I've also tried this (Where "treeNodes" is the actual TreeList inside the TreeListLookupEdit):
treeNodes.FocusedNode = fn;
But, when I run this piece of code, it works:
treeListLookupEdit1.ShowPopup();
treeListLookupEdit1.Properties.TreeList.FocusedNode = fn;
treeListLookupEdit1.ClosePopup();
So, how to avoid using the ShowPopup?
Update
It seems, you should set EditValue
treeListLookupEdit1.EditValue = NodeId
You need to set up TreeListLookUpEdit.Properties.DisplayMember property and TreeListLookUpEdit.Properties.ValueMember property.
Set the TreeListLookUpEdit.Properties.DisplayMember property to the column that you want to display in your TreeListLookupEdit and TreeListLookUpEdit.Properties.ValueMember to ID column and use TreeListLookUpEdit.EditValue to focus node.
After that you can do something like this:
treeListLookupEdit1.EditValue = fn.GetValue("YourIDColumn");
Here is example with DataTable as data source:
var dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Parent_ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Rows.Add(1, null, "1");
dataTable.Rows.Add(2, null, "2");
dataTable.Rows.Add(3, null, "3");
dataTable.Rows.Add(4, 1, "1.1");
dataTable.Rows.Add(5, 1, "1.2");
dataTable.Rows.Add(6, 3, "3.1");
dataTable.Rows.Add(7, 3, "3.2");
dataTable.Rows.Add(8, 5, "1.2.1");
var treeListLookUpEdit = new TreeListLookUpEdit();
var properties = treeListLookUpEdit.Properties;
properties.DataSource = dataTable;
properties.DisplayMember = "Name";
properties.ValueMember = "ID";
var treeList = properties.TreeList;
treeList.KeyFieldName = "ID";
treeList.ParentFieldName = "Parent_ID";
treeList.RootValue = DBNull.Value;
Controls.Add(treeListLookUpEdit);
treeListLookUpEdit.Size = treeListLookUpEdit.CalcBestSize();
If you set EditValue property of this treeListLookUpEdit object for example to 5 then you will see "1.2" text in control and node with such text will be focused:
treeListLookUpEdit.EditValue = 5;
I would like to know how the XtraGrid and the BandedGrid play togehter and are bound to the underlaying data. The documentation has some explanatory tl;dr-text but i am missing a full working example to set it up in code. So it took me about 2 hours to figure it out. Based on this blog entry
i would like to post my answer here.
If there is a better way to put the pieces together as in my answer below i would love to know about it.
First you have to know that you can bind a plain DataTable to the XtraGrid and that the creation of the banded grid is independent.
Below you can see a new instance of XtraGrid is created. It MainView is set to be a BandedGridView
private void LoadAndFillXtraGrid() // object sender, EventArgs e
{
grid = new DevExpress.XtraGrid.GridControl();
grid.Dock = DockStyle.Fill;
// set the MainView to be the BandedGrid you are creating
grid.MainView = GetBandedGridView();
// set the Datasource to a DataTable
grid.DataSource = GetDataTable();
// add the grid to the form
this.Controls.Add(grid);
grid.BringToFront();
}
Above the line grid.MainView = GetBandedGridView(); set a BandedGridView as the MainView of the Xtragrid. Below you see how to create this BandedGridView
//Create a Banded Grid View including the grindBands and the columns
private BandedGridView GetBandedGridView()
{
BandedGridView bandedView = new BandedGridView();
// Set Customer Band
SetGridBand(bandedView, "Customer",
new string[3] { "CustomerId", "LastName", "FirstName" });
SetGridBand(bandedView, "Address", new string[3] { "PLZ", "City", "Street" });
return bandedView;
}
To set up the GridBand you have to create a GridBand and attach it to the bandedGridView by calling bandedView.Columns.AddField for each column
private void SetGridBand(BandedGridView bandedView, string gridBandCaption
, string[] columnNames)
{
var gridBand = new GridBand();
gridBand.Caption = gridBandCaption;
int nrOfColumns = columnNames.Length;
BandedGridColumn[] bandedColumns = new BandedGridColumn[nrOfColumns];
for (int i = 0; i < nrOfColumns; i++)
{
bandedColumns[i] = (BandedGridColumn)bandedView.Columns.AddField(columnNames[i]);
bandedColumns[i].OwnerBand = gridBand;
bandedColumns[i].Visible = true;
}
}
The DataSource can be a plain DataTable that contains some columns. If the name of the column in the datatable matches the names of the BandedGridColumn the will be automatically mapped. As you can see i added a column NotMapped in the datatable which is not visible in the screenshot above:
private DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("CustomerId", typeof(Int32)),
new DataColumn("NotMapped", typeof(Int32)),
new DataColumn("LastName", typeof(String)),
new DataColumn("FirstName", typeof(String)),
new DataColumn("PLZ", typeof(Int32)),
new DataColumn("City", typeof(String)),
new DataColumn("Street", typeof(String))
});
dt.Rows.Add(1, 0, "John", "Barista", 80245, "Manhatten", "Broadway");
dt.Rows.Add(2, 0, "Mike", "Handyman", 87032, "Brooklyn", "Martin Luther Drive");
dt.Rows.Add(3, 0, "Jane", "Teacher", 80245, "Manhatten", "Broadway 7");
dt.Rows.Add(4, 0, "Quentin", "Producer", 80245, "Manhatten", "Broadway 15");
return dt;
}
If someone has a more elegant way to put the pieces together i would love to know about it.
I'm creating a part of my window in code. For a combobox I do this:
ObservableCollection<ParamClassOption> options = new ObservableCollection<ParamClassOption>(
context.ParamClassOptions.Where(x => x.IDParamClass == val.CompTypeParam.IDParamClass));
ComboBox combobox = new ComboBox();
combobox.Name = "combobox" + val.CompTypeParam.ParameterName.Replace(" ", "");
combobox.ItemsSource = options;
combobox.SelectedValuePath = "IDParamClass";
combobox.DisplayMemberPath = "OptionName";
if (val.ParamClassOption != null)
{
combobox.SelectedValue = val.ParamClassOption.IDParamClassOption;
}
layoutitem.Content = combobox;
I'm able to select an item from the list and save it to the database. The problem that I have is to show the saved value again upon retrieving the values back from the database. Any idea why it's not showing? val.ParamClassOption.IDParamClassOption in the second to last line above has the correct value when the record is retrieved to be displayed.
i think you forgot to bind your selected value
var binding = new Binding {Path = new PropertyPath("IDParamClassOption"), Mode = BindingMode.TwoWay, Source = val.ParamClassOption};
combobox.SetBinding(ComboBox.SelectedValueProperty, binding);
hope this helps
Thanks for the help. I ended up using a completely different approach by adding the items to the combo-box one by one. I then set the selected item to previously added value (using the Text property). Here is what my code looks like now:
if (controlType == "Combobox")
{
ComboBox combobox = new ComboBox();
combobox.Name = "combobox" + val.CompTypeParam.ParameterName.Replace(" ", "");
ObservableCollection<ParamClassOption> options = new ObservableCollection<ParamClassOption>(
context.ParamClassOptions.Where(x => x.IDParamClass == val.CompTypeParam.IDParamClass));
combobox.Items.Clear();
foreach (ParamClassOption option in options)
{
ComboBoxItem item = new ComboBoxItem();
item.Content = option.OptionName;
combobox.Items.Add(item);
}
combobox.Text = val.ParamClassOption.OptionName;
layoutitem.Content = combobox;
}
Later when reading the value from the combobox to save to the database I did this:
ObservableCollection<ParamClassOption> option = new ObservableCollection<ParamClassOption>(
context.ParamClassOptions.Where(o => o.IDParamClass == value.CompTypeParam.IDParamClass).Where(o => o.OptionName == combobox.Text));
value.IDParamClassOption = option[0].IDParamClassOption;