DataValue field value C# - c#

I am doing a project and can't make a Seletect Value from a list get the right value.A list is generated from a class UserDepartment,in this class I have this basically:
public class UserDepartment
{
public long ID { get; set; }
public string Description { get; set; }
public User UserResponsible { get; set; }
}
The Problem is that I need a value from the class userResponsible,inside there is a value called EDV, I need this value but I don't know how to get.See this image below:
If I use " ListBeneficiaryArea.DataValueField = "ID"; ",I get the ID value normally,but i cant get the EDV value, I already tried "EDV","UserResponsible.EDV" and "UserResponsible"but it didn't work.
There is other way for me to get the UserResponsible.EDV in the DataValueField?
After the change in DataSource i received this error:

You can change your DataSource to the following:
ListBeneficiaryArea.DataSource = from a in lstBUAreas
select new { ID, EDV = a.UserResponsible.EDV };
Then you can do:
ListBeneficiaryArea.DataTextField = "ID";
ListBeneficiaryArea.DataValueField = "EDV";

Related

Accessing properties of object in SelectList in C#

I'm a newbie here. I'm trying to create a dropdown list (SelectList) which would display the location of shops in my ASP.NET MVC application.
The Shop class has a Location property which in turn has a Name property:
public class Shop
{
public int Id { get; set; }
public Location Location { get; set; }
}
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
}
The Name property is a string.
I tried to use this code:
var shops = _applicationDbContext.Shops
.Include(x => x.Location).ToList();
var mwc = new Create
{
ShopSL = new SelectList(shops, nameof(Shop.Id), nameof(Shop.Location.Name))
};
But I get an error:
Object reference not set to an instance of an object
If I remove the Shop.Location.Name and change it to Shop.Location only, the dropdown list displays the object instead, something like Namespace.Data.Location.
I would like the dropdown list to be able to display the name of the location.
Could someone point me to the right direction please? Thank you.
try this
var shops = _applicationDbContext.Shops
.Select (i=> new
{
Id=i.Id
Name=i.Location.Name
}).ToList();
var mwc = new Create
{
ShopSL = new SelectList(shops,"Id","Name") ;
};

Get the column name from list

I would like to fetch the column names from List. As I have a class
public class DetailView
{
public string SiteName { get; set; }
public string ItemType { get; set; }
public string AssetStorage { get; set; }
}
and in some method in controller am filling the data into Session. Now I want to get the column names for some reason. Am putting my session data into that list.
List<DetailView> objgrdDtls = new List<DetailView>();
objgrdDtls = (List<DetailView>)Session["datasetVal"];
I would like to have the Column name. Pleas note by doing the below code i got the value of that particular column name. restult1 has the column value.
var result1 = objgrdDtls.Where(p => p.SiteName.ToLower().Contains(txt1));
But all i need is Column name. So how do i get that.
stringColumnname = objgrdDtls.get(columnaname => some filter)?
Is this is the way how to get column names?
Not sure how to get the column name one by one?
Thanks.
You can use reflection to get the column name. Here is the runnable example to get the column name of DetailView.
using System;
using System.Reflection;
public class DetailView
{
public string SiteName { get; set; }
public string ItemType { get; set; }
public string AssetStorage { get; set; }
}
public class Example
{
public static void Main()
{
DetailView fieldsInst = new DetailView();
// Get the type of DetailView.
Type fieldsType = typeof(DetailView);
PropertyInfo[] props = fieldsType.GetProperties(BindingFlags.Public
| BindingFlags.Instance);
for(int i = 0; i < props.Length; i++)
{
Console.WriteLine(" {0}",
props[i].Name);
}
}
}
Var result1 will give the list of DetailView Objects which meet the condition "SiteName.ToLower().Contains(txt1)". It will not be a value. Can you please clarify what do you mean by column value. However, For selecting a particular column value, You can append ".Select(p => p.AssetStorage)".

Creating custom linq column names in list

We are returning a generic List to a GridView, which then auto generates columns to show a report:
//Generate List
List<Stock> allStock = blStock_Collection.getAll();
//export custom view for report datagrid
return (from myStock in allStock
select new
{
myStock.Category,
myStock.Description,
myLowStock.UnitPrice,
myLowStock.CurrentQuantity
});
Our client has asked that we now provide multi-lingual support on our site (English & Polish), specifically the column headers on grids. We would therefore need to get rid of the auto generate option on all our data grids, and add in the translations manually for each column.
I was wondering is there a way to do this when generating the datasource, which would save us a hell of a lot of time, e.g. changing this line:
myStock.Category,
to something like:
languagePack.Category = myStock.Category,
This is of course throwing a 'Invalid anonymous type member declarator' error. Any advice?
Maybe I misread something, but if you just want to put your language into it (LanguagePack is just a class holding your values):
class LanguagePack
{
public int Category { get; set; }
public string Description { get; set; }
public decimal UnitPrice { get; set; }
public int CurrentQuantity { get; set; }
public int LanguageName { get; set; }
}
return (from myStock in allStock
select new LanguagePack
{
Category = myStock.Category,
Description = myStock.Description,
UnitPrice = myLowStock.UnitPrice,
CurrentQuantity = myLowStock.CurrentQuantity,
LanguageName = "Polish" // or english or whatever... maybe LanguageId or something corresponding to your model
});

Properties and objects C#

I want to make public property that gets and sets object I get from datagrid.
I have datagrid that has 3 columns with text and 3 columns with checkboxes.
When I check one of the checkbox I get a value of whole row in list like this:
var selectedItemsController = MyObsCollection.Where(n => n.Controller).ToList();
That is a list of objects with all 3 string values (and all 3 bool values of checkboxes) that are in same row where checkboxes in column named Controller is.
MyObsCollection is also public property for ObservableCollection and they are defined like this:
ObservableCollection<RowData> _obsCollection =
new ObservableCollection<RowData>();
public ObservableCollection<RowData> MyObsCollection
{
get { return _obsCollection; }
}
RowData is my class that caries a model I need and it's defined like this:
public class RowData : INotifyPropertyChanged
{
public string Type { get; set; }
public string MapTo { get; set; }
public string Name { get; set; }
public bool Controller { get; set; }
public bool Service { get; set; }
public bool Injection { get; set; }
public RowData(string type, string mapTo, string name)
{
Type = type;
MapTo = mapTo;
Name = name;
}
What I'm trying to do is to make a public property for that list of objects(in selectedItemsController) so I can use it in other class.
For example I was doing this with name for some Area that is also part of WindowsForm. I was taking the name from some text box and making public property like this:
public string AreaName
{
get { return AreaNameValue.Text; }
set { AreaNameValue.Text = value; }
}
And after that I was able to do this in other class:
var areaName = areaDialog.AreaName.Trim();
So finally my question is does someone know how can I make same public property for DataRow object if the name of DataGrid is for example: tabela? Is there something already defined in DataGrid that I can use? (like 'Text' property is for InputTextBoxes).
You can set your selectedItemsController as the datacontext of your DataGrid. Then you can set and get values on the selectedItemsController itself.

How do I bind a list to a ddl?

I have a list and I am trying to add the data to the ddl. It returns data, (namespace.List). But there is something I am missing... any suggestions?
public List<getBranch> Branch { get; private set; }
...
getBranch(user.code);
ddlOption.DataSource = Branch;
ddlOption.DataBind();
All you're missing is to tell the dropdown what property of getBranch to show as text and what to use as value:
ddlOption.DataTextField = "propertyOfgetBranchToShowAsText";
ddlOption.DataValueField = "propertyOfgetBranchToUseAsValue";
ok, here's how the code should look like (i think):
your Branch class properties:
public int BranchValue {get;set;}
public string BranchText {get;set;}
...
public List<Branch> branchesToShow { get; private set; }
...
branchesToShow = getBranch(user.code); //get the list of branches
ddlOption.DataTextField = "BranchText"
ddlOption.DataValueField = "BranchValue";
ddlOption.DataSource = branchesToShow;
ddlOption.DataBind();

Categories