UltraGrid add BindingList<MyClass> and map property names to column names - bindinglist

Is it possible to map the property names of my class to the column names of the UltraGrid control?
MyClass is for example a user class:
class User
{
public int Id { get; set; }
public string Name { get; set; }
}
BindingList<User> myList = new BindingList<User>();
private void Form1_Load(object sender, EventArgs e)
{
this.ultraGrid1.DataSource = myList;
}
This works, but the colum names will be overwritten with the property names of my class.
Is there a way to map the property names to colum names?
I tried this by using DataBindings, but this did not work.

Why yes, yes there is!
The DisplayName attribute in the System.ComponentModel namespace:
class User
{
[DisplayName("Identifier")]
public int Id { get; set; }
[DisplayName("First Name")]
public string Name { get; set; }
}
You can also do the following in the InitializeLayout event:
e.Layout.Bands(0).Column("Name").Header.Caption = "First Name";

Related

how to make DisplayName in MVC model?

while I was building MVC5 model with SQLite. the time span is tricky so I stored the tick instead. but then I get trouble on display name for it.
[PetaPoco.TableName("UserStatus")]
[PetaPoco.PrimaryKey("iUserId", AutoIncrement = true)]
public class User
{
[Column]
[DisplayName("User ID")]
public int iUserId { get; set; }
[Column]
[DisplayName("User Name")]
public string sUserName { get; set; }
//[DisplayName("Average Time")] this line won't work
public TimeSpan tsAvgTime;
[Column]
public long longAvgTimeTick
{
get { return tsAvgTime.Ticks; }
set { tsAvgTalkTime = new TimeSpan(value); }
}
}
so what should I do to add a display name instead of change the display on the view.
I suspect it is because you have not created Property but a Field.
It should be a property to make it work.
Change it to :
[DisplayName("Average Time")]
public TimeSpan tsAvgTime {get;set;}
Now this should work.
EDIT:
Just noticed you are using a backing field. You need to apply the attribute on the property which is longAvgTimeTick here and make the field private i.e. private TimeSpan tsAvgTime
so it should be :
private TimeSpan tsAvgTime;
[Column]
[DisplayName("Average Time")]
public long longAvgTimeTick
{
get { return tsAvgTime.Ticks; }
set { tsAvgTalkTime = new TimeSpan(value); }
}

ABP - How delete depended items from entity - entity framework

i have this entity Page:
public class Page : FullAuditedEntity<int, User>, IMultiLanguageEntity<PageTranslation>
{
public string Name { get; set; }
public string Content{ get; set; }
public Page()
{
Translations = new List<PageTranslation>();
}
public virtual IList<PageTranslation> Translations { get; set; }
}
And entity PageTranslation:
[Table("PageTranslations")]
public class PageTranslation : Entity<int>, IEntityTranslation<Page>
{
public Page Core { get; set; }
public int CoreId { get; set; }
public string Language { get; set; }
public string Name { get; set; }
public string Content{ get; set; }
}
I want to update page entity with updated values and tranlsations, so I call this service:
public void UpdatePage(UpdatePageInput input)
{
var item = _pageRepository.Get(input.Id);
item.Content = input.Content;
item.Description = input.Description;
item.Title = input.Title;
item.Name = input.Name;
item.Translations.Clear(); // there is a problem
item.Translations.addRange(input.Translations);
}
When I call item.Translations.Clear() method I got this exception:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
How to solve this in ABP - http://www.aspnetboilerplate.com/?
Thanks for help !
While assuming u have a DBContext:
U can declare the on delete action:
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Entity<Page>()
.HasOptional(a => a.Translations)
.WithMany()
.WillCascadeOnDelete(true);
}
did you try
_pageRepository.DeleteAll();

Get field from different model in MVC 5

I have a model linked to a second table:
public class Rock
{
public int ID { get; set; }
[ForeignKey("Con")]
public int ConID { get; set; }
public virtual Con Con { get; set; }
}
public class Con
{
public int ID { get; set; }
public virtual ICollection<Rock> Rock{ get; set; }
[Required]
[RegularExpression(#"^[0-9A-Za-z '']+$")]
[StringLength(200, MinimumLength = 3)]
public string Name { get; set; }
}
In my control, I have a 'create' action:
// GET: Rock/Create/3337
[Route("Rock/Create/{ConID?}")]
public ActionResult Create(int? ConID)
{
var rock= new Rock();
rock.ConID = (int)ConID;
return View(rock);
}
I'd like to get the con name from that table and send it to the view. At this point it doesn't know the name because there's no 'rock' record linking it yet.
Any suggestions? Thanks in advance!
If you have created a strongly typed view with Rock as Type, you need to either add 'Con Name' property to Rock Type or else you need to create a new Type and add the data to this Type which you want to pass to the View.
public class NewType
{
public int ConID { get; set; }
public string ConName { get; set; }
}
Add the data you want to pass in this Type and return view with this object:-
public ActionResult Create(int? ConID)
{
var newType= new NewType();
newType.ConID = (int)ConID;
newType.ConName = "XYZ";
return View(newType);
}
The two suggestions I would give are to pass Con.name to the view in the viewbag or to create a viewmodel, as Rahul suggested, that combines the properties of different classes that are needed for that particular view.

Initilialize Navigation Property when creating new Entity with Breeze

I have simple Model defined as
public class Project
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int StatusId { get; set; }
public virtual Status Status { get; set; }
}
public class Status
{
public int Id { get; set; }
public string Name { get; set; }
}
So, currently If I try to create a new Project Entity with breeze, it initializes the navigation property "Status" with null. How can I initialize it with default value? Note that, I don't want any binding with drop down field for this field at least for create operation.
function createNewProject() {
return manager.createEntity('Project');
}
You can define a custom constructor for the Project entity type and set a default StatusId value in there. The Status navigation property will be set to the relevant status entity (assuming the entity is in the breeze cache). For example:
function Project() {
this.StatusId = desiredDefaultStatusEntityId;
}
var manager = new breeze.EntityManager('...');
manager.metadataStore.registerEntityTypeCtor('Project', Project);
Take a look at Extending entities for some more information on extending entities.

Adding dynamic attributes to a model

I can't wrap my mind around this issue and haven't found the correct search keys yet:
I would like to have several categories of items in which all items have specific attributes. Those attributes (text fields, dropdowns, or checkboxes) should be added to a category and I want to edit and save those attributes for each item.
I'm working with MVC 4 and code-first EF5. How can I implement this?
My first approach were several classes like Text, Dropdown that were inherited from an abstract Attribute class and a Category class like this:
public class Category
{
[Key]
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Item> Items { get; set; }
public virtual ICollection<Attribute> Attributes { get; set; }
}
But then I had no idea to proceed. Am I on the right way or completely wrong? Can someone give me a few hints I can search for?
Edit
Ultimately I'm trying to build a list of hifi devices. Speakers have different attributes than amplifier and those have different attributes to tape recorders. I would like to give a unified look for the details of each device and pre-define specific attributes to that category with an additional free-for-all text area. Speaker XYZ is my item, Speaker my category and dB an attribute.
Ok so this question is basically about the data design.
First, I assume that the rule is:
One item has one category
One category has many attributes
One item has many attributes associated with the category
For rule no.1, it is good enough in your design. (simplified example)
public class Category{
public IEnumerable<Item> Items{get;set;}
}
public class Item{
public Category Category{get;set;}
}
Its clear enough.
For rule no.2, I think you should make a CategoryAttribute class. It holds the relation between one to many Category and Attribute. Basically, CategoryAttribute is a master, whereas the children will be ItemAttribute.
public class Category{
public IEnumerable<CategoryAttribute> CategoryAttributes{get;set;}
}
public class CategoryAttribute{
public Category Category{get;set;}
public string CategoryName{get;set;}
public string DefaultValue{get;set;} // maybe a default value for specific
// attribute, but it's up to you
public IEnumerable<ItemAttribute> ItemAttributes{get;set;}
}
The IEnumerable<ItemAttribute> is the one to many relation between category attribute and item attribute.
For rule no.3, the the ItemAttribute described in rule no.2 will be represented attribute owned by each item.
public class Item{
public IEnumerable<ItemAttribute> ItemAttributes{get;set;}
}
public class ItemAttribute{
public Item Item {get;set;} // it owned by one attribute
public CategoryAttribute{get;set;} // it owned by one category attribute
}
I don't quite sure about how to represent relation or primary and foreign key in code first. Hopefully I can enhance my answer if needed (and if I able). But hopefully my illustration about relations and the class designs for each objects.
I think something like this may work for you...
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Item> Items { get; set; }
public virtual ICollection<Attribute> Attributes { get; set; }
}
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public virtual ICollection<ItemAttribute> ItemAttributes { get; set; }
}
public class Attribute
{
public int AttributeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<ItemAttribute> ItemAttributes { get; set; }
}
public class ItemAttribute
{
public int ItemId { get; set; }
public int AttributeId { get; set; }
public Item Item { get; set; }
public Attribute Attribute { get; set; }
public string Value { get; set; }
public int ValueInt{ get; set; }
// etc.
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ItemAttribute>()
.HasKey(x => new { x.ItemId, x.AttributeId });
modelBuilder.Entity<ItemAttribute>()
.HasRequired(x => x.Item)
.WithMany(x => x.ItemAttributes)
.HasForeignKey(x => x.ItemId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<ItemAttribute>()
.HasRequired(x => x.Attribute)
.WithMany(x => x.ItemAttributes)
.HasForeignKey(x => x.AttributeId)
.WillCascadeOnDelete(false);
// AttributeCategories is created for you - but you can do the same as ^ above to customize
// just change 'ICollection<Category> Categories' to collection of 'ItemAttribute'
}
// use it like e.g.
var item = new Item { Name = "ItemTest", };
var attribute = new Attribute { Name = "attributeTest", };
item.ItemAttributes = new List<ItemAttribute>
{
new ItemAttribute { Item = item, Attribute = attribute, Value = "test", },
};
var category = new Category
{
Name = "cat1",
Items = new[]
{
item,
new Item{ Name = "Item1", },
new Item{ Name = "Item2", },
new Item{ Name = "Item3", },
new Item{ Name = "Item4", },
new Item{ Name = "Item5", },
},
Attributes = new[]
{
attribute,
new Attribute{ Name = "att1", },
new Attribute{ Name = "att2", },
}
};
db.Categories.Add(category);
db.SaveChanges();
var categories = db.Categories.ToList();
ItemAttribute is used to connect and store values.
And you're going to need to further adjust as per your requirements.
I actually never worked with code first approach, but I can give you some idea about how this scenario can be handled...To me, it looks that Item is the major one instead of Category. So you can have this structure...
public class Category
{
[Key]
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string CategoryDescription { get; set; }
// use attributes here if you want them for Category
//public Dictionary<string, string> ItemnAttributes { get; set; }
}
public class MyItem
{
[Key]
public int ItemId { get; set; }
public string ItemName { get; set; }
public string ItemDescription { get; set; }
public Category ItemnCatagory { get; set; }
public Dictionary<string, string> ItemnAttributes { get; set; }
}
Hope this helps..

Resources