How does one get row data from a Silverlight DataGrid? - silverlight-4.0

In Silverlight, how does one get row data from a DataGrid that is full of data?
I have gotten this far (in a method that receives a button click on a row(:
DataGridRow item = (DataGridRow)dg.SelectedItem;
Now, how do I get the individual components of the item that I guess is the selected row?
Help me out here. How do you bind an observablecollection to the grid?
How do you use the cast system when you cast to the object?
When I read the data into the grid, I used this class:
public class Data
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public bool Available { get; set; }
public int index_1 { get; set; }
public int index_2 { get; set; }
public int index_3 { get; set; }
public int index_4 { get; set; }
public int index_5 { get; set; }
public int index_6 { get; set; }
public int index_7 { get; set; }
public int index_8 { get; set; }
public int index_9 { get; set; }
public int index_10 { get; set; }
public int index_11 { get; set; }
public int index_12 { get; set; }
public int index_13 { get; set; }
public int index_14 { get; set; }
public int index_15 { get; set; }
}
So how so I cast when I read back out
This does not work:
Data _mydata = new Data();
YValue = (_mydata.index_1)dg.SelectedItem;
This does not work:
YValue = (index_1)dg.SelectedItem;
This does not work:
YValue = (Data().index_1)dg.SelectedItem;

DataGridRow item = (DataGridRow)dg.SelectedItem;
int index1 = ((Data)item).index_1;
That will give you the value of the first index.

If you have bound a ObservableCollection<Foo> to your grid, your selected item can just be cast into your object - (Foo)dg.SelectedItem
EDIT-- UPDATE TO ANSWER UPDATED QUESTION
The simple answer is, if you are not using MVVM (which I assume by your post your not), in the code behind create a collection (preferably ObservableCollection) of Data and set the grids itemsource property to your collection
public ObservableCollection<Data> MyCollection{get;set;}
void SetGridItemsSource()
{
// populate your collection here, then use the below line to associate it with your
// grids itemssource
MyGrid.ItemsSource = MyCollection;
}
public void GetSelectedItem()
{
//Simply cast the selected item to your type
Data selectedItem = (Data)MyGrid.SelectedItem;
}

Related

How to store value in variable joining two tables

I am still learning SQL, but as I got stuck in these doubt I wrote it here.
Actually I want print a table for list of details which hold a column for "Description". Now this data is retrieving from tblBillDetails. and in tblBillDetails there is no column or field of "Description. Now there is another field which is tblProduct here I have column "Name" & "Id".
So I want something like "Description = tblProduct.Name where tblBillDetails.FunctionId == tblProduct.Id
code--->
.cs file
public class BillDetail
{
public long Id { get; set; }
public long BillId { get; set; }
public int FunctionType { get; set; }
public long FunctionId { get; set; }
public long VatId { get; set; }
public int Quantity { get; set; }
public int Points { get; set; }
public decimal TotalValue { get; set; }
public decimal FreeValue { get; set; }
public DateTime SaleDateTime { get; set; }
public long BalanceId { get; set; }
public decimal RunningBalance { get; set; }
}
repositoy .cs file
public Task<BillDetail> SingleAsync(Guid shardKey, long BillDetailId)
{
return _billDetailDataAccess.SingleAsync(shardKey, BillDetailId);
}
DataAccess .cs file
public async Task<BillDetail> SingleAsync(Guid shardKey, long BillDetailId)
{
string sql = #"SELECT [Id]
,[TenantClientId]
,[BillId]
,[FunctionType]
,[FunctionId]
,[VatId]
,[Quantity]
,[Points]
,[TotalValue]
,[FreeValue]
,[SaleDateTime]
,[BalanceId]
,[RunningBalance]
,[Archived]
FROM [dbo].[tblBillDetail]
WHERE [tblBillDetail].[Id] = #BillDetailId
;";
await _context.OpenConnectionForKeyAsync(shardKey);
return await _context.Connection.QueryFirstOrDefaultAsync<BillDetail>(sql, new { BillDetailId });
}

Dynamic Xamarin Image Source Binding

I have the same question please..
I have the url this way:
mydomain.com/gallery/COMPANY_GUID/items/ITEM_GUID/600x400.png
where COMPANY_GUID and ITEM_GUID comes from the database here:
public class ItemDetails
{
public int item_id { get; set; }
public int item_guid { get; set; }
public int merchant_guid { get; set; }
public string item_name { get; set; }
public string item_description { get; set; }
public string item_price { get; set; }
}
How can I set the Images source {Binding item_image} where the item_image should be a dynamic variable comes from the above?
public class ItemDetails
{
public int item_id { get; set; }
public int item_guid { get; set; }
public int merchant_guid { get; set; }
public string item_name { get; set; }
public string item_description { get; set; }
public string item_price { get; set; }
//Add this field like this.
public string item_image {
get {
return string.Format("mydomain.com/gallery/{0}/items/{1}/600x400.png", merchant_guid, item_guid);
}
}
}
Try to add one field item_image as given in the above code. This will return what you need, dynamically. Hope this will help you.

Creating a score sheet in SQL

On my website, I have a score sheet that the user fills out. They will USUALLY have 10 items on a score sheet and each item will have its own score. I am wanting to design this in the best way for future expandability. What If I want to have more or less items on the score sheet in the future?
Here is what I have now:
public class Scoresheet712Item
{
public int ScoresheetItemId { get; set; }
public int ScoresheetId { get; set; }
public int DistanceAway { get; set; }
public int score { get; set; }
}
Is it better to do this or is it better to have all 10 scores and distances on the same row? I would rather have them in the same row because I can pull that one row directly in as a model. I originally thought to do it this way so I could easily vary how many slots there are on the score sheet, but it doesn't seem like there is much benefit really, specifically because I am using MVC development and I will always need the entire score sheet.
Please help, can I have all the data in one row for a score sheet and that be good practice?
Here is what I am trying to propose, it would be a little different though because each score DOES have a different purpose.:
public class Scoresheet
{
public int ScoresheetId { get; set; }
public int DistanceAway1 { get; set; }
public int score1 { get; set; }
public int DistanceAway2 { get; set; }
public int score2 { get; set; }
public int DistanceAway3 { get; set; }
public int score3 { get; set; }
public int DistanceAway4 { get; set; }
public int score4 { get; set; }
public int DistanceAway5 { get; set; }
public int score5 { get; set; }
public int DistanceAway6 { get; set; }
public int score6 { get; set; }
public int DistanceAway7 { get; set; }
public int score7 { get; set; }
public int DistanceAway8 { get; set; }
public int score8 { get; set; }
public int DistanceAway9 { get; set; }
public int score9 { get; set; }
public int DistanceAway10 { get; set; }
public int score10 { get; set; }
}
Like this?
public class Scoresheet712
{
public int Scoresheet712ID { get; set; }
public virtual ICollection<Scoresheet712Item> Scoresheet712Items { get; set; }
}
I am inclined to agree with #Stephen Muecke to be flexible and extendable you really need two sql tables.
public class Scoresheet
{
public int ScoresheetId { get; set; }
public string ScoresheetName { get; set; }
public ICollection<ScoresheetItem> ScoresheetItems { get; set; }
}
And
Public class ScoresheetItem
{
public int ScoresheetItemId { get; set; }
public int Score { get; set; }
public int Distance { get; set; }
//Navigation properties
public int ScoresheetId { get; set; }
public Scoresheet Scoresheet { get; set; }
{
This will let you build a new Scoresheet as needed with as many or as few items as you want. The example below shows the usage.
//Create a new Scoresheet
Scoresheet scoresheet712 = new Scoresheet()
{
ScoresheetName = "Score Sheet 712",
ScoresheetItems = new List<ScoresheetItem>()
};
//Add a ScoresheeItem to Scoresheet
scoresheet712.ScoresheetItems.Add(new ScoreSheetItem()
{
Score = 10,
Distance = 150
});
Remember that on your View you do not need to use your Data Model you can always add a Data Transfer Object (Scoresheet_DTO) and make the flat structure for your score sheet if it really does make displaying it easier/better, just be sure to use nullable integers.
UPDATE MANY-TO-MANY RELATIONSHIP
For a many-to-many relationship if setup correctly in code it will automatically create the joining table. The Scoresheet class would remain the same but the ScoresheetItem class would have a small change to the Relationship/Navigation properties.
Public class ScoresheetItem
{
public int ScoresheetItemId { get; set; }
public int Score { get; set; }
public int Distance { get; set; }
//Navigation properties
public ICollection<Scoresheet> Scoresheet { get; set; }
{
For further help I would recommend taking a look at this Entity Framework Tutorial

ASP.NET MVC ViewModel is null on Post Back - sometimes

depending on my input in an HTML form (6 or 6.5 - or in general, integer VS float) I get or don't get the following exception (it works with int and doesn't work with float):
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Boolean'.
The ViewModel of my View is null and the problem gets visible in a custom template which expects a bool value, but gets null instead. We use ASP.NET MVC 4 with .Net 4.0, C# and Razor Templates.
After several hours debugging I came to the following conclusion(s):
Post Form-Data are identical (except the one property which is different, but still look correct)
The execution order is somehow weirdly different:
for int I get Application_BeginRequest->Filter which runs through my attributes->Action->View Rendering OR Redirect (everything Normal)
for float I get Application_BeginRequest->Filter which runs through my attributes->View Rendering->END WITH AN EXCEPTION and Empty ViewModel
I have checked it dozen times -> If I pass float the View somehow gets rendered without any Action (which I would have seen) to be executed (and of course the breakpoints were the same all the time). Unfortunately I couldn't see anything in the StackTrace anymore once the View got rendered.
the ViewModel of my View is:
public class JabCommonViewModel
{
public int JAB_ID { get; set; }
[UIHint("Checkbox")]
public bool JAB_gesperrt { get; set; }
[UIHint("Checkbox")]
public bool JAB_Kontrolliert { get; set; }
public int e001 { get; set; }
public string e002 { get; set; }
public int e005 { get; set; }
[UIHint("Checkbox")]
public bool e013 { get; set; }
public bool e014 { get; set; }
public short? e015 { get; set; }
public bool? e149 { get; set; }
public int? e649 { get; set; }
public int? e310 { get; set; }
public int? LastJabe311 { get; set; }
public int jabIdE013 { get; set; }
public int jabIdPrev { get; set; }
public int updCnt { get; set; }
public int checks { get; set; }
public bool calculateInEur { get; set; }
public FormViewModel AktivaPassiva { get; set; }
public FormViewModel GuV1GuV2 { get; set; }
public FormViewModel GuV3 { get; set; }
public ActsFormViewModel ActsForm { get; set; }
public CommonDataViewModel CommonDataForm { get; set; }
public CompanyHeadViewModel CompanyHeadForm { get; set; }
public FacilitiesOverviewModel FacilitiesOverview { get; set; }
}
public class FormViewModel
{
public string ShowAllCaption { get; set; }
public string HideAllCaption { get; set; }
public string CurrentCaption { get; set; }
public string PreviousCaption { get; set; }
public bool HasPreviousData { get; set; }
public IEnumerable<FieldViewModel> Fields { get; set; }
public FormViewModel()
{
Fields = new FieldViewModel[0];
}
}
public class FieldViewModel
{
public string Name { get; set; }
public string Title { get; set; }
public object Value { get; set; }
public bool IsDisabled { get; set; }
public bool IsCollapsible { get; set; }
public bool IsSpecialCase { get; set; } // used currently to expand/collapse groups on second level
public FieldViewModel Previous { get; set; }
public Category DataCategory { get; set; }
public IEnumerable<FieldViewModel> Related { get; set; }
public FieldViewModel()
{
Related = new FieldViewModel[0];
}
public FieldViewModel(string name, string title, object value, bool isDisabled, Category dataCategory = Category.None, bool isCollapsible = true)
{
Name = name;
Title = title;
Value = value;
IsDisabled = isDisabled;
DataCategory = dataCategory;
IsCollapsible = isCollapsible;
Related = new FieldViewModel[0];
}
}
....
The Post-Back Action is
public ActionResult Edit(JAB2 jab)
{
ComponentFactory.Logger.Debug("Edit with JAB2");
....
}
public class JAB2 : JAB
{
public int jabIdE013 { get; set; }
public JAB LastJab { get; set; }
public int checks { get; set; }
}
public class JAB : BaseModel
{
public JAB()
{
}
public bool e116 { get; set; }
public bool e117 { get; set; }
public bool e118 { get; set; }
public bool e119 { get; set; }
public bool e120 { get; set; }
public bool e121 { get; set; }
public bool e122 { get; set; }
public bool e123 { get; set; }
public bool e124 { get; set; }
public bool e125 { get; set; }
public short? e126 { get; set; }
... /* 100 more properties */ ...
[LocalizedDisplayName("e751", NameResourceType = typeof(Resources.ModelPropertyNames))]
public float? e751 { get; set; } /* the property which works as int but not as float */
}
The Post-Back is actually to the link
/JAB/Edit/
Still the correct method get's executed when e751 (the special property) has an integer value.
Also we use the autoNumeric-JavaScript Plugin on that field. Also we use the plugin with other fields but so far found the error only with this one. As well, we have one Workstation where we aren't able to reproduce the error so it occurs on 2 out of 3 workstations + test server.
So far, nothing I've found explains the fact that it works sometimes.
Thank you very much for taking the time and reading my post.
Do you have any Ideas what could be wrong or what I could check?

Accessing data in a ViewModel

I have the following entity framework code snippet which has a "Groups" table and a child "ApplicationsGroupsLK" table that contains an ApplicationID field that I need.
IEnumerable<Groups> Groups = DbContext.Groups.Include("ApplicationsGroupsLK").Where(p => p.GroupNumber > 0);
The child data comes back obviously in a collection.
I basically need to display the parent data along with the child ApplicationID field (many Applications to 1 Group).
In my MVC View, what should my ViewModel look like that would contain the parent and child data coming back that I need in order that I can properly bind it to a grid?
Second post:
Further, the following model was generated from Entity Framework:
public partial class Project
{
public Project()
{
this.TimeTrackings = new HashSet<TimeTracking>();
}
[DataMember]
public short ProjectID { get; set; }
[DataMember]
public short CustomerID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public short CategoryID { get; set; }
[DataMember]
public short PriorityID { get; set; }
[DataMember]
public short StatusID { get; set; }
[DataMember]
public Nullable<decimal> Quote { get; set; }
[DataMember]
public string Notes { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public virtual Category Category { get; set; }
[DataMember]
public virtual Customer Customer { get; set; }
[DataMember]
public virtual Priority Priority { get; set; }
[DataMember]
public virtual Status Status { get; set; }
[DataMember]
public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
}
You can see that TimeTrackings is the child table of Project. You can also see that CategoryID, CustomerID, PriorityID, and StatusID are foreign keys that the parent table has. In this case, I'm only interested in the CategoryID FK.
I haven't done this yet (not at my machine at home), but when I get the data into this model, what would actually be contained in the public virtual Category Category field? Since it's not a collection, what data is returned in this field after the query executes.
Third post:
Telerik asp.net for mvc syntax for DB call:
IEnumerable<Groups> GroupList = db.GetGroups();
return View(new GridModel<Groups>
{
Data = GroupList
});
Fourth post:
Trey, below is the code I modified for my purposes and was hoping you can check it over before I implement it. I think I undersand it and seems great...
public partial class Project
{
public Project()
{
this.TimeTrackings = new HashSet<TimeTracking>();
}
[DataMember]
public short ProjectID { get; set; }
[DataMember]
public short CustomerID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public short CategoryID { get; set; }
[DataMember]
public short PriorityID { get; set; }
[DataMember]
public short StatusID { get; set; }
[DataMember]
public Nullable<decimal> Quote { get; set; }
[DataMember]
public string Notes { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public short ApplicationID { get; set; }
[DataMember]
public string ApplicationName { get; set; }
[DataMember]
public virtual Category Category { get; set; }
[DataMember]
public virtual Customer Customer { get; set; }
[DataMember]
public virtual Priority Priority { get; set; }
[DataMember]
public virtual Status Status { get; set; }
[DataMember]
public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
public ProjectModel(Project project)
{
ProjectID = project.ProjectID;
CustomerID = project.CustomerID;
Name = project.Name;
Description = project.Description;
CategoryID = project.CategoryID;
PriorityID = project.PriorityID;
StatusID = project.StatusID;
Quote = project.Quote;
Notes = project.Notes;
CreatedDate = project.CreatedDate;
UpdatedDate = project.UpdatedDate;
ApplicationID = project.ApplicationsGroupsLK.ApplicationID;
ApplicationName = project.ApplicationsGroupsLK.ApplicationName;
}
// Neat Linq trick to convert database query results directly to Model
public static IList<ProjectModel> FlattenToThis(IList<Project> projects)
{
return projects.Select(project => new ProjectModel(project)).ToList();
}
}
Fifth post:
using (wmswebEntities DbContext = new wmswebEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<Projects> projects = DbContext.Projects.Where(p => p.GroupNumber > 0);
IList<ProjectModel> results = Project.FlattenToThis(projects);
return results
}
Sixth post
namespace CMSEFModel
{
using System;
using System.Collections.Generic;
public partial class GroupModel
{
public GroupModel()
{
this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
}
public int GroupNumber { get; set; }
public string GroupName { get; set; }
public int GroupRank { get; set; }
public bool ActiveFlag { get; set; }
public System.DateTime DateAdded { get; set; }
public string AddedBy { get; set; }
public System.DateTime LastUpdated { get; set; }
public string LastUpdatedBy { get; set; }
// Application - Lazy Loading population
public int ApplicationID { get; set; }
// UsersGroupsLK - Lazy Loading population
public int UserNumber { get; set; }
public string UserID { get; set; }
public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }
public GroupModel()
{}
public GroupModel(GroupModel group)
{
GroupNumber = group.GroupNumber;
GroupName = group.GroupName;
ActiveFlag = group.ActiveFlag;
DateAdded = group.DateAdded;
AddedBy = group.AddedBy;
LastUpdated = group.LastUpdated;
LastUpdatedBy = group.LastUpdatedBy;
UserNumber = group.UsersGroupsLKs.
}
// Neat Linq trick to convert database query results directly to Model
public static IList<GroupModel> FlattenToThis(IList<GroupModel> groups)
{
return groups.Select(group => new GroupModel(group)).ToList();
}
}
}
Seventh Post - this is the model I am having trouble with about the errors I previously posted. Trey, if you could help, I WOULD REALLY APPRECIATE IT.... I'm "dead in the water" unless I can get this part working.
namespace CMSEFModel
{
using System;
using System.Collections.Generic;
public partial class Group
{
public Group()
{
this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
}
public int GroupNumber { get; set; }
public string GroupName { get; set; }
public int GroupRank { get; set; }
public bool ActiveFlag { get; set; }
public System.DateTime DateAdded { get; set; }
public string AddedBy { get; set; }
public System.DateTime LastUpdated { get; set; }
public string LastUpdatedBy { get; set; }
// Application - Lazy Loading population
public int ApplicationID { get; set; }
// UsersGroupsLK - Lazy Loading population
public int UserNumber { get; set; }
public string UserID { get; set; }
public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }
public GroupModel(Group group)
{
GroupNumber = group.GroupNumber;
GroupName = group.GroupName;
ActiveFlag = group.ActiveFlag;
DateAdded = group.DateAdded;
AddedBy = group.AddedBy;
LastUpdated = group.LastUpdated;
LastUpdatedBy = group.LastUpdatedBy;
UserNumber = group.UsersGroupsLKs.
}
// Neat Linq trick to convert database query results directly to Model
public static IList<GroupModel> FlattenToThis(IList<Group> groups)
{
return groups.Select(group => new GroupModel(group)).ToList();
}
}
}
Eight post:
using (wmswebEntities DbContext = new wmswebEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = true;
DbContext.Database.Connection.Open();
List<Groups> myGroups = new List<Groups>();
var myGroups = from p in DbContext.Groups
where p.ActiveFlag = true
select new
{
p.Groups.ApplicationName,
p.Groups.GroupName,
p.Groups.GroupRank,
p.Groups.ActiveFlag,
p.Groups.DateAdded,
p.Groups.AddedBy,
p.Groups.LastUpdated,
p.Groups.LastUpdatedBy,
p.Groups.ApplicationsGroupsLK.ApplicationID,
p.Groups.UsersGroupsLK.UserNumber
};
return myGroups;
}
This will take a slight tweak in thinking. The Grid will only accept a flat model. This type of question is asked often, here is a starter answer: Kendo UI Grid - How to Bind to Child Properties
If that does not help, post more of your code here and I can work through it with you.