I have such class
public class AuthorModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public DateTime RegistrationDate { get; set; }
}
I have created GridControl in wpf devexpress, and set ItemsSource to List. (MyGrid.ItemsSource = myAuthorsList)
The question is: How to control column names, for example, I want grid control to show Your Name instead of Name as in a class?
I want to change column names after setting ItemsSource
You can use several approaches to define column headers in DevExpress GridControl:
Define columns in XAML and set the Header property:
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Id" Header="Author ID"/>
</dxg:GridControl.Columns>
Subscribe to the GridControl.AutoGeneratedColumns event, iterate through the GridControl.Columns collection and set column Headers in code.
Use data annotation attributes in your model and activate the EnableSmartColumnsGeneration option:
public class AuthorModel
{
[Display(ShortName = "Author ID")]
public int Id {
get;
set;
}
//...
}
<dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True">
<!--...-->
</dxg:GridControl>
Note that you need to add a reference to the System.ComponentModel.DataAnnotations assembly to use these attributes
Related
My issue is that I have 2 picker controls. 1 of these picker controls is bound to list a and one of these picker controls is bound to list b. Both of these controls display the data I need them to with the correct display bindings working fine.
My problem is when I bind the selectedItem property, it works for list a but doesn't for list b. I've checked that the code is literally a like for like copy of each other.
I have been using the syncfusion Combobox but switched to the picker as I thought there was an issue here but there isn't. Whatever is happening is totally down to whatever I'm doing.
The usage scenario is that I bring down a list of payment types from my API and populate a picker based on this. This works.
The datasource for my main view contains an ID. When I am modifying a record, I run a method called update to find the selectedItem. I'm not happy with this approach and would be interested to see what other people use.
The update method gets the datasources for the pickers and finds what I would expect to be the selected item. This works fine also but doesn't bind.
[Serializable]
public class PaymentInformation :BaseModel
{
public int? ID { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public int? PaymentTypeId { get; set; }
public string PaymentTo { get; set; }
public string Location { get; set; }
public string Notes { get; set; }
public PersonInformation PersonBudget { get; set; }
public decimal AmountPaid { get; set; }
public decimal AmountReceived { get; set; }
public double TotalHours { get; set; }
public void Update(ObservableCollection<ResourceInformation> resources , ObservableCollection<PaymentTypeInformation> paymentTypes)
{
if(PaymentTypeId != null) this.PaymentTypeInformation1 = paymentTypes?.FirstOrDefault((paymentType) => paymentType.ID == PaymentTypeId.Value);
this.Resource = resources?.FirstOrDefault((resource) => resource.ResourceId == PersonBudget?.ID);
}
private PaymentTypeInformation _paymentTypeInformation;
private PaymentTypeInformation PaymentTypeInformation1 { get { return _paymentTypeInformation; } set { _paymentTypeInformation = value; OnPropertyChanged(nameof(PaymentTypeInformation1)); } }
private ResourceInformation _resource;
public ResourceInformation Resource { get { return _resource; } set { _resource = value; OnPropertyChanged(nameof(Resource)); } }
}
The underlying xaml is:
<Label Grid.Row="8" Grid.Column="0" Text="Payment Type:" />
<Picker BackgroundColor="White" Grid.Row="8" Grid.Column="1" ItemsSource="{Binding PaymentTypesDataSource}" ItemDisplayBinding="{Binding Path=DisplayText}" IsEnabled="{Binding IsProcessing, Converter={StaticResource reverseBoolConvertor}}" SelectedItem="{Binding DataSource.PaymentTypeInformation1, Mode=TwoWay}" />
The expected result is that the drop down initializes with the selectedItem which it doesn't (in one usage scenario - the other one works fine).
Couldn't see the wood for the trees.
private PaymentTypeInformation PaymentTypeInformation1
{
get
{
return _paymentTypeInformation;
}
set
{
_paymentTypeInformation = value;
OnPropertyChanged(nameof(PaymentTypeInformation1));
}
}
Can't bind to a private property - changed to public and immediately work. Stuck on this for a day as bonkers as that is to believe.
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
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));
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.
I used http://json2csharp.com to generate the C# class from the below JSON string.
{
"stationArr":[
{
"id":"9",
"name":"name9",
"sidebar":{
"original":"http://myurl.com/station_images/5/5_s.png",
"m":"http://myurl.com/station_images/5/m/5_s_m.png",
"s":"http://myurl.com/station_images/5/s/5_s_s.png"
}
},
{
"id":"3",
"name":"name3",
"sidebar":{
"original":"http://myurl.com/station_images/5/5_s.png",
"m":"http://myurl.com/station_images/5/m/5_s_m.png",
"s":"http://myurl.com/station_images/5/s/5_s_s.png"
}
]
"stationUrlMap":{
"9":"http://myurl.com/9_64",
"3":"http://myurl.com/3_64",
}
}
The generated classes are (I created different .cs for each class.
public class Sidebar
{
public string original { get; set; }
public string m { get; set; }
public string s { get; set; }
}
public class StationArr
{
public string id { get; set; }
public string name { get; set; }
public Sidebar sidebar { get; set; }
}
/*public class StationUrlMap
{
public string __invalid_name__9 { get; set; }
public string __invalid_name__3 { get; set; }
}
*/
public class StationList
{
public List<StationArr> stationArr { get; set; }
// public StationUrlMap stationUrlMap { get; set; } Dicarded it
}
I have discarded the StationUrlMap as I don't need it.
I am using the following code to create the Object
string resultString = sd.ReadToEnd();
StationList stations = JsonConvert.DeserializeObject<StationList>(resultString);
Debug.Writeline(stations.stationArr.Count); // gives Output 9 Which is correct.
I just don't know how to display the List of stations in the UI (using ListBox). Please guide me in the right direction.
Create a ListBox in the XAML
<ListBox x:Name="ListBoxStations" Height="500" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="450">
<Image Source="{Binding Path=sidebar.original}"/>
<TextBlock Text="{Binding Path=name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And then in your cs file, after the json conversion do the following
StationList stations = JsonConvert.DeserializeObject<StationList>(resultString);
ListBoxStations.ItemsSource = stations.stationArr;
The above XAML code is just a sample, change based on your requirements.
There are two ways.
First you can set the listbox's ItemSource property to the list you just created.
Second, You can create an ObservableCollection of thing in your list, and bind the observable collection to the listbox's ItemSource property. Add items in the Observable Collection and these will be displayed in your listbox.
The second way gives you more control over the items in the lisbox. You can remove or add elements in the observable collection and that will reflect in the listbox contents. The first method will make the contents of the list read-only, and you will not be able to add or remove items from it.
http://msdn.microsoft.com/en-us/library/ms668604.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox%28v=vs.95%29.aspx