I have got a response back from an API which contains a list of hotels, which I want to store in an array and then display this list in a table, something like this.
//Get a response
IRestResponse<RootObject> searchResponse = client.Execute<RootObject>(searchRequest);
var hotelList = searchResponse.Data.HotelListResponse.HotelList.HotelSummary.ToArray();
foreach (var hotelSummary in hotelList.ToArray())
{
TextBoxResults.Text = hotelSummary.hotelId.ToString();
TextBoxResults.Text = hotelSummary.shortDescription.ToString();
TextBoxResults.Text = hotelSummary.address1.ToString();
TextBoxResults.Text = hotelSummary.address2.ToString();
TextBoxResults.Text = hotelSummary.city.ToString();
}
At the moment, what this is doing is only returning one of the ten results from the list of results and not all 10.
What I'm trying to do is put the list into an array and then display the results in a table in my ASP.net page, the list will not contain more than ten results.
You've chosen the wrong Control to show your data. Textbox can only show 1 value not a list of values. I guess you are using forms. then you have grid view.
use it like this:
MyGridview.DataSource = hotelList ;
MyGridview.DataBind();
Take a look at this link from asp.net
Update
also take a look at this
C# - Populating Gridview
The reason why it's happening is because you're just overwriting the TextBoxResults. Seems like you don't have a Datagridview or some sort.
So I used the following as suggested:
//Get a response
IRestResponse<RootObject> searchResponse = client.Execute<RootObject>(searchRequest);
var hotelList = searchResponse.Data.HotelListResponse.HotelList.HotelSummary.ToArray();
foreach (var hotelSummary in hotelList.ToArray())
{
GridViewResults.DataSource = hotelList;
GridViewResults.DataBind();
}
This returns everything, and opens up the list and all the element provided from the API, it would be good to return only certain columns, instead of all of them.
Related
I ask a similar question but I asked it slightly wrong, thus asking a second question more geared at my issue.
I have a list with custom fields/columns in it and I need to retrieve that data that is in one of the custom fields/columns. However, when I am retrieving the items of the list none of my custom columns/fields are not showing up in the fields list of the object.
List<QueryOption> queryExpandFields = new List<QueryOption> { new QueryOption("$expand", "fields") };
IListItemsCollectionPage items = await GraphClient.Sites[sharepointSessionId].Lists[listName].Items.Request(queryExpandFields).GetAsync();
The code above grabs the items in the list however, as mentioned, none of my columns/fields are in the list.
For example
List Name: ListNameTest
Columns: TestColumn1, TestColumn2, TestColumn3, TestColumnId
Data: TestColumn1: "Dog", TestColumn2: 2021, TestColumn3: "January", TestColumnId: 001
In my code I need to grab the Items in the list and retrieve the data that is in the TestColumnId for example.
On the graph-explorer site I have tried the following end point. And in here I do see my custom column list in the columns, but when I look at the Items, I do not see the column listed in the fields section of the item (or anywhere else in the item)
GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}?expand=columns,items(expand=fields)
My overall purpose is to grab the items from the list, do a parse on the TestColumnId as this is data that is populated from a database. Grab the unique id that I populated in my custom column (TestColumnId) to get the ID value of that item in the list. This ID value will then be passed to a secondary List to populate the Lookup id that has the first list as a sub list in the second list.
Now I have all of this code set up, I have it working if I hard code the values, but I need this one last piece to work.
Edit:
I finally figured out what the issue was! in the QueryOptions I had to do a selection on the exact column i wanted.
var queryOptions = new List<QueryOption>()
{
new QueryOption("expand", "fields(select=ColumnName)")
};
var items2 = await GraphClient.Sites[siteid].Lists[listid].Items
.Request(queryOptions)
.GetAsync();
var queryOptions = new List<QueryOption>()
{
new QueryOption("expand", "fields(select=ColumnName)")
};
var items2 = await GraphClient.Sites[siteid].Lists[listid].Items
.Request(queryOptions)
.GetAsync();
I have 2 listBoxes with BindingLists as their data sources. The idea is to make a List builder (as MSDN names it), where first listBox shows currently added columns and second listBox shows the rest of available columns. First list contains ViewColumn objects, while the other list contains strings.
I load chosen columns into first listBox from database and then I want to load the rest of available columns into the second listBox (the list itself comes from another place in database). Considering there are no limits on the number of columns, I want to do that in the fastest way possible - what would that be?
Here's some code to visualize that:
ViewTable _view;
BindingList<ViewColumn> _viewColumns = new BindingList<ViewColumn>();
BindingList<string> _detailsColumns = new BindingList<string>();
void CustomInitialize()
{
_view = //get view and its columns
_viewColumns = new BindingList<ViewColumn>(_view.Columns);
listBox_CurrentColumns.DataSource = _viewColumns;
listBox_CurrentColumns.DisplayMember = "Name";
var detailsTable = //get the list of available columns
foreach (var row in detailsTable)
{
//TODO: if _viewColumns does not contain this value, add it to _detailsColumns
_detailsColumns.Add(row.ColumnName);
}
listBox_AvailableColumns.DataSource = _detailsColumns;
}
I think you want to do something like:
_detailsColumns = _allColumns.Except(_viewColumns.Select(c => c.Name))
This should get you all entries in the _allColumns collection excluding the entries in the _viewColumns collection.
I assume here that _allColumns contains the overall collection of possible columns.
I am populating a listBox at runtime from a database as follows:
List<FILE_REPORT_TYPES> ReportTypes = GetReportTypesFromDatabase(ReportMappingIds)
BindingList<FILE_REPORT_TYPES> pbReportTypesBindingList = new BindingList<FILE_REPORT_TYPES>(ReportTypes);
listBoxReports.DataSource = ReportTypesBindingList;
listBoxReports.DisplayMember = "REPORT_DESCRIPTION";
listBoxReports.ValueMember = "REPORT_ID";
I then would like select multiple items on the listBox when running the windows form and retrieve each individual Value of my selections. If only one selection is made one could do the following:
listBoxReports.SelectedValue;
I would like to do the following:
var list = listBoxReports.SelectedValues;
However this is not allowed i.e. "SelectedValues" does not exist.
Some people are erroneously suggesting that in this particular case SelectedIndices may be used. It cannot be used, I am trying to retrieve the "VALUE". This cannot be done (in this particular case):
listBox.Items[i].Value;
I think the solution should be along the lines of:
foreach(var line in listBox.Items)
{
var res= ((SOME CASTING)line).Value;
}
To get the selected items you have 2 options
a.) ListBox.SelectedIndices which returns the indices of the selected items which you then need to use to look up in the Items property what the value is or
b.) ListBox.SelectedItems which returns you a collection with the selected items themselves (be aware that it is an objectlist so you need to transform the items into your appropriate datatype).
Edit: With the additional information the following is possible
List<FILE_REPORT_TYPES> mySelectedList = new List<FILE_REPORT_TYPES>();
foreach (Object selectedItem in ListBox.SelectedItems)
{
mySelectedList.Add( ((FILE_REPORT_TYPES)selectedItem) );
}
You can use ListBox.SelectedIndices or ListBox.SelectedItems.
If you want to get all selected-items, you can let the foreach cast:
foreach(FILE_REPORT_TYPES frt in listBox.SelectedItems)
{
// ...
}
or if you want to get the ReportID into a list with the help of LINQ:
List<decimal> reportIds = listBox.SelectedItems.Cast<FILE_REPORT_TYPES>()
.Select(frt => frt.REPORT_ID)
.ToList();
Alternative to the selected value you could do the following
listBoxReports.SelectedItems;
Answer (the casting is the trick):
List<decimal> reportIds = new List<decimal>();
foreach(var line in listBoxReports.SelectedItems)
{
reportIds.Add(((PB_FILE_REPORT_TYPES)line).REPORT_ID);
}
You may try like below
List<FILE_REPORT_TYPES> reportList = new List<FILE_REPORT_TYPES>();
foreach(var item in listBox.SelectedItems)
{
reportList.Add((FILE_REPORT_TYPES)item);
}
Alright so when the web part loads I pull from a list like so in the CreateChildControls() function:
String userGroup = GetProfileProperty(SPContext.Current.Web.CurrentUser.LoginName);//get user login's newsgroup // "All";
if (!string.IsNullOrEmpty(userGroup) && !userGroup.Equals("All"))
{
qry.RowLimit = 5;
camlquery = "<Where><Contains><FieldRef Name='Intended_x0020_Audience' /><Value Type='MultiChoice'>" + userGroup + "</Value></Contains></Where><OrderBy><FieldRef Name='Start_x0020_Date' Ascending='False' /></OrderBy>";
qry.Query = camlquery;
items = newsList.GetItems(qry);
//Print items inside a ajax panel....
}
else
{
qry.RowLimit = 5;
camlquery = "<OrderBy><FieldRef Name='Start_x0020_Date' Ascending='False' /></OrderBy>";
qry.Query = camlquery;
items = newsList.GetItems(qry);
////Print items inside a ajax panel....
}
I do not use paging yet, but I have stored the query list in a var items. When the user clicks a button the update panel clears out the current controls and does another query to get the new items, but this time I use paging.(see below)
int lastID = items[items.Count - 1].ID;//last news item id of current page
//Get Web Context
SPWeb web = SPContext.Current.Web;
//Clear old panel
UpdatePanel up = (UpdatePanel)FindControl("Panel");
up.ContentTemplateContainer.Controls.Clear();//clear container of old news items
SPQuery qry = new SPQuery();//get query
qry.RowLimit = 5;
string camlquery = "<OrderBy><FieldRef Name='Start_x0020_Date' Ascending='False' /></OrderBy>";
qry.Query = camlquery;
SPListItemCollectionPosition position = new SPListItemCollectionPosition("Paged=TRUE&p_ID=" + lastID.ToString());
qry.ListItemCollectionPosition = position;
items = web.Lists["News Wire"].GetItems(qry);// newsList.GetItems(qry);
//print items inside ajax panel....
It does change the page, but it returns more items than necessary. I'll get five when the webpart first loads, but when I click the button that triggers the event to get more list records, I get the first item of the next page and also the first page's first 3 items. Note: My list only has six items in it. I limited my RowLimit to 5, but when I load the second page I get 4 items and 3 from the first page.
If you're paging forwards, you can pass the ListItemCollectionPosition from your first query's results (SPListItemCollection) to the next query - you don't have to mess about constructing it by hand.
You'll need to store it somewhere after you get the first set of results - I've used ViewState before, you could use a hidden control in the update panel as well.
When you're paging backwards, you do need to create the string as you are doing. You also need to include a key/value to indicate that you're going backwards, and you need to include an indication of your sort. (You might be able to get this from the auto-generated forward-paging value.) (I think missing the sort is why your forwards paging isn't working, too.)
This is the resource I used when I was trying to get this working myself, although it's a little hazy now.
I have a GridView which I have a List bound to - I want to be able to allow filter of the data based on multiple CheckBoxLists.
For arguments sake let's say I have a List of Jobs, and I want to filter the jobs by code - So I would have a CheckBoxList like
C#
ASP.NET
php
F#
etc..
If someone selects multiple codes, how do you pass the values into a List to rebind to the GridView? Is there a way to pass values as an array? Or maybe a comma seperated string?
Any examples (I'm a C# boy) would be greatly appreciated, and I hope I have explained it properly :S
use an ObservableCollection<T> . it automatically allows the gridview to "observe" that the underlying datasource has changed and thus update itself.
wherever you do your filtering for the gridview you have to build the list manually before you filter.
var languages = new List<string>();
foreach (ListItem item in cblLanguages.Items)
{
if (item.Selected)
{
languages.Add(item.Value);
}
}
then when you filter you can do something like (example using linq2sql)
var jobs = db.Jobs.Where(x => langauges.Contains(x.LanguageCode));
gvJobs.DataSource = jobs;
gvJobs.DataBind();
I'm not sure I completely understand your question. But I often do the following to get ListItems into a form queryable via LINQ to objects:
var items = cblLanguages.Items.Cast<ListItem>();
// Selected Items
var selectedItems = items.Where(li => li.Selected);
// Item's containing 'C'
var itemsWithC = items.Where(li => li.Text.Contains("C"));
// Values between 2 and 5
var itemsBetween2And5 = from li in items
let v = Convert.ToInt32(li.Value)
where 2 <= v && v <= 5
select li;