Change value of data inside databinding before displaying to textbox - c#

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));

Related

Bind List<object> to GridControl with custom columns

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

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

How to bind a stacklayout in a viewcell in Xamarin Forms?

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

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.

How to databind to a label with a custom object collection in C# winforms

I'm using a VB power packs data repeater control. I need to bind a list of custom objects to labels inside the repeater. The following code works except for the Tip.User.UserName binding.
How can I bind to a property of an Inner class like Tip.User.UserName
public interface ITip
{
DateTime Date { get; set; }
int Id { get; set; }
int UserId { get; set; }
User User { get; set; }
Group Group { get; set; }
}
public interface IUser
{
string DisplayName { get; set; }
string UserName { get; set; }
}
List<Tip> currentTips = SearchTips(toolTxtSearch.Text, Convert.ToInt32(toolCmbTipGroups.ComboBox.SelectedValue));
lblTipId.DataBindings.Add(new Binding("Text", currentTips, "Id"));
lblTipUser.DataBindings.Add(new Binding("Text", currentTips, "User.UserName")); // this line doesnot work !!!
repeater.DataSource = currentTips;
Totally depends on what error you're getting because nested property syntax should work for WinForm bindings.
As a work around (maybe the DataRepeater isn't using regular binding mechanisms?) add a property to you ITip implementation to wrap it up:
public string UsersUserName
{
get { return User != null ? User.UserName : null; }
}
Edit: Also don't forget to implement INotifyPropertyChanged on your data objects if you want bindings to update when values do. In this case, send property changed events for both the User property of ITip implementations and the UserName of the IUser implementation.

Categories