How to bind a stacklayout in a viewcell in Xamarin Forms? - c#

I like the idea of having a ListView and just bind data and specify a DataTemplate for list item layout. But here is where I dont get it. Say I have:
public class Order
{
public int Id {get; set; }
public string CustomerName { get; set; }
public List<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public string ItemName { get; set; }
}
So far so good. Now I want to populate a list in ListView which looks as follows:
(Orderid)
ItemName, ItemName
(Orderid)
ItemName, ItemName, ItemName
and so on.
Edit: I dont want to list the Order Id but I want to be able to fetch it on a click event in the list.
I cant find anywhere If I can bind a collection in a ViewCell. How should I go about this? Is there a way to have nested ViewCells in this matter?
/Mike

I presume from your post that you are wanting the CustomerName to almost act as a section header.
You should look at the example here for creating grouped list views in Xamarin.Forms and you will be able to recreate the desired visual:
https://github.com/xamarin/xamarin-forms-samples/tree/master/LabelledSectionsList
Good luck

Related

How to show selected item in Syncfusion DropDownList control

I am working on an ASP.NET MVC application using Syncfusion controls. I have a drop down list in my view. The model has a property "Categories" which is a List of type Category.
public class Category
{
public int XKategorieId { get; set; }
public int? Id { get; set; }
public string Hinweis { get; set; }
public string Kategorie { get; set; }
}
The model of the view also has a property "IdFromCategory". The model is:
public class ReportModel
{
public int? IdFromCategory { get; set; }
public List<Category> Categories { get; set; }
}
I am showing all the categories in the drop down list by setting the "DataSource" of "DropDownList". Now, my issue is that i want to show an item selected in the "DropDownList" when the view loads and that selected item will be the one with "Id" equals to "IdFromCategory ".
#Html.EJS().DropDownList("KundenBetreuung").DataSource(Model.Categories).Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "Kategorie", Value = "Id" }).Value(Model.IdFromCategory.ToString()).Width("100%").Render();
This is my code, i am unable to set the selected item in the "DropDownList"
In the Razor code, you have filled the value property with Model.IdFromCategory.ToString() (a string) where the declared properties IdFromCategory and Id both are integers. This mismatching type is the cause of the issue in your end and the value is not being set. To successfully set the value make sure that the value provided is available in the datasource and its type matches as well.
Suggest changing the code as follows
#Html.EJS().DropDownList("KundenBetreuung").DataSource(Model.Categories).Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "Kategorie", Value = "Id" }).Value(Model.IdFromCategory).Width("100%").Render();
Check the following example for further reference
Example

MVC model with a property for related models

Imagine there is a model defined as:
public class Book
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Guid> RelatedBooks { get; set; } // List<Book> would be more ideal.
}
I'm trying to add a field in both the Create and Edit views whereby a user will be able to search for related items, in this example by name, and then add them to the new/current item as a related item.
I think this might be possible with a partial view + jQuery, where the partial view acts a search box and returns a list of items. Then with some buttons and jQuery magic these could be added to the current model upon submission of the parent form.
I'm not against using jQuery if it's the only option, but I was wondering if there were any alternatives?
If this is too vague I'm happy to add some more specific details, I've tried search for similar questions but I'm either using the wrong search terms or there aren't any.
Any help would be really appreciated - I'd rather not go down the jQuery + partial view route if there is a cleaner alternative.
First off, you need to create a view-model for what you're trying to achieve that will hold both the list of related books, but also a list of all books we can select.
public class CreateBookViewModel {
public Guid Id { get; set; }
public string Name { get; set; }
public List<Guid> RelatedBooks { get; set; }
public List<Book> AllBooks { get; set; }
}
Populate AllBooks in the Controller and return the view model. Then you could create some magic, maybe with jQuery, to have a select list with all books, that when selected are added to the list of related books by Id. At least you wouldn't need partial views, if you don't want any more functionality than just selecting one or more books.

Filling Product Information list to a class variable

How can I fill products class below with products queried from SQL Server?
public class products
{
public item[] item { get; set; }
}
public class item
{
public string id { get; set; }
public string name { get; set; }
public string price { get; set; }
}
What I am trying to achieve is a variable with type products that contain a set of products.
products productset = new products();
I am hoping to work with the variable as follow:
lableName.Text = productset.item[4].name;
Please let me know how to fill such a variable with data from SQL.
To create a set of products you need to create a list. So your products variable will look like this:
List<products> products = new List<products>();
If you want a list of items inside the product object, then you can leave it like this.
I don't exactly know what you mean with 'fill such a variable with data from SQL'. Do you already have the code to query the database, or is that the part that you don't understand/need? Please provide some more info so we can help you out. In case you have code, please post it here.

Change value of data inside databinding before displaying to textbox

I have the same set up of databindings as this one
Visual Studio Winform designer: Set DataBindings on current control BindingSource
But I don't know how to change the value of let's say I have 2 models:
class Receipt {
public int ProductId { get; set; }
public double Price { get; set; }
//etc...
}
class Product {
public int ProductId { get; set; }
public string ProductName { get; set; }
//etc...
}
My datagrid shows the Receipt model and when one is selected, my textboxes shows other details which are not displayed in the datagrid.
Now my problem is I need to display on my text box the ProductName instead of the ProductId.
I am using Entity Framework Code First.
Please help...
TIA.
Since you are using Entity Framework and you have Product property in Receipt class, you can load Product with Receipt, for example this way:
this.receiptBindingSource.DataSource = db.Receipt.Include("Product").ToList();
You can set the databinding of your TextBox to bind to Product.ProductName property using the designer or code:
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.receiptBindingSource, "Product.ProductName", true));

Why is RadGridView trying to create columns for my entity's collection properties as well?

I have a RadGridView which I populate like this:
public MainWindow()
{
InitializeComponent();
Occupations = _dbContext.Occupations.ToList();
DataGrid.ItemsSource = Occupations;
}
The Occupation entity class looks something like this (properties removed for brevity):
public partial class Occupation : XTimeEntity
{
public Occupation()
{
this.BUSCOMPLs = new List<BUSCOMPL>();
this.EMPLOYEEs = new List<EMPLOYEE>();
}
public short OccupationCodeId { get; set; }
public short SiteCodeId { get; set; }
public short OccupationActive { get; set; }
public string OccupationName { get; set; }
public virtual ICollection<BUSCOMPL> BUSCOMPLs { get; set; }
public virtual ICollection<EMPLOYEE> EMPLOYEEs { get; set; }
}
When the code runs, on the last line of MainWindow I get the errors (SqlException):
Invalid object name 'dbo.OccupationBUSCOMPL'
Invalid object name 'dbo.OccupationEMPLOYEE'
Surely these collections should have nothing to do with the column creation of the RadGridView? It should only include OccupationCodeId through OccupationName. I have never found this issue on other grids. What could cause this?
According to Telerik...
The default behavior of RadGridView control is to generate its columns
automatically based on the underlying data source. This means that
when you set the ItemsSource of the RadGridView control to a
collection of employees for example, a separate column will be created
for each one of the public properties of your Employee object. This is
the default behavior and it does not require any additional efforts
from your side. Just set the ItemSource of your RadGridView and you
are ready.
source: http://www.telerik.com/help/wpf/gridview-columns-defining-columns.html
Therefore all your properties will be automatically generated. If you read this page (and the rest of the documentation) it states quite clearly how to generate the columns you need.
A quick edit.. This guide will also help point you to a QUICK way to "Not auto-generate a column" http://www.telerik.com/help/wpf/gridview-prevent-column-autogenerate.html
Cheers.

Categories