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.
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;
I'm trying to collect my url and the description of the url stored in a column of a list from sharepoint and i don't know how to collect the URL value.
This is my code :
var queryResultSaleListItems = clientContext.LoadQuery(listData);
clientContext.ExecuteQuery();
//Read the Data into the Object
var TipsList = from Tips in queryResultSaleListItems
select Tips;
ObservableCollection<Tips> colTips = new ObservableCollection<Tips>();
//Read Every List Item and Display Data into the DataGrid
foreach (SPSClient.ListItem item in TipsList)
{
var tips = new Tips();
tips.TitleTip = item.FieldValues.Values.ElementAt(1).ToString();
tips.App = item.FieldValues.Values.ElementAt(4).ToString();
//should collect the url
tips.URL = item.FieldValues.Values.ElementAt(5).ToString();
//should collect the description of the url
tips.URLdesc = item.FieldValues.Values.ElementAt(5).ToString();
colTips.Add(tips);
}
ListboxTips.DataContext = colTips;
In my expression its >
((Microsoft.SharePoint.Client.FieldUrlValue)(item.FieldValues.Values.ElementAt(5))).Url
((Microsoft.SharePoint.Client.FieldUrlValue)(item.FieldValues.Values.ElementAt(5))).Description
Thanks for your help,
Use FieldUrlValue for getting hyperlink field in Client Object Model.
Use Following Code:
string server = "siteURL";
var ctx = new ClientContext(server);
var web = ctx.Web;
var list = web.Lists.GetByTitle("CustomList");
var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(listItemCollection);
ctx.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem listItem in listItemCollection)
{
string acturlURL = ((FieldUrlValue)(listItem["URL"])).Url.ToString(); // get the Hyperlink field URL value
string actdesc = ((FieldUrlValue)(listItem["URL"])).Description.ToString(); // get the Hyperlink field Description value
}
I am new to ExtJS. In examples, I do not find any one similar to my situation: I have an Array of Object like bellow:
arr = new Array();
obj = new Object();
obj.StringField = "Field1";
obj.IntField = 1;
arr.push(obj);
obj = new Object();
obj.StringField = "Field2";
obj.IntField = 3;
arr.push(obj);
obj = new Object();
obj.StringField = "Field3";
obj.IntField = 5;
arr.push(obj);
How to put this in to GridPanel?
You can create a grid panel with an ArrayReader : http://www.sencha.com/learn/Manual:Data:Readers:ArrayReader
There's also an example: http://dev.sencha.com/deploy/ext/examples/grid/array-grid.html