How do I bind a list to a ddl? - c#

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

Related

How to add items to existing list of objects?

I have three classes:
public class M2ArticleMain
{
public int Id { get; set; }
public List<M2ArticleAttributeWeb> Attribut_Web { get; set; }
}
public class M2ArticleAttributeWeb
{
public int Web_Id { get; set; }
public M2ArticleTmpMainSkus Variants { get; set; }
}
public class M2ArticleTmpMainSkus
{
public DateTime TimeAdded { get; set; }
public List<string> Skus { get; set; }
}
And I have two Lists in my code like this:
List<M2ArticleMain> data = new List<M2ArticleMain>();
List<M2ArticleAttributeWeb> attb = new List<M2ArticleAttributeWeb>();
In some part of my code firstly I (from foreach loop) add data to attb list where I add only only some data (because I don't have all data at this point), like this:
...
attb.Add(new M2ArticleAttributeWeb
{
Web_id = item.Id, //(item is from foreach loop)
Variants = null //this is **importat**, I left null for later to add it
});
Next, after I fill attb, I add all this to data list:
...
data.Add(new M2ArticleMain
{
Id = item.Id_Pk, //this is also from foreach loop,
Attribut_Web = attb //now in this part I have only data for Web_id and not Variants
}
Now my question is How to Add items later to data list to object Variants?
Something like this:
data.AddRange( "how to point to Variants" = some data);
The M2ArticleAttributeWeb type holding your Variants property is the member of a collection. That is, there are potentially many of them. You can reference an individual Variants property like this:
data[0].Attribut_Web[0].Variants
But you need to know which items you want to add map to which data and Attribut_Web indexes/objects in order to assign them properly. That probably means another loop, or even a nested loop. That is, you can see all of your Variants properties in a loop like this:
foreach(var main in data)
{
foreach(var attrw in main)
{
var v = attrw.Variants;
// do something with v
Console.WriteLine(v);
// **OR**
attrw.Variants = // assign some object
}
}
It's also much better practice to create your collection properties with the object, and then give them private set attributes:
public class M2ArticleMain
{
public int Id { get; set; }
public List<M2ArticleAttributeWeb> Attribut_Web { get; private set; } = new List<M2ArticleAttributeWeb>();
}
public class M2ArticleAttributeWeb
{
public int Web_Id { get; set; }
public M2ArticleTmpMainSkus Variants { get; set; }
}
public class M2ArticleTmpMainSkus
{
public DateTime TimeAdded { get; set; }
public List<string> Skus { get; private set; } = new List<string>();
}
Now instead of assigning Attribut_Web = attb, you would need to .Add() to the existing List.

get specific properties within the generic list using linq

I have generic list foo as shown below
var foo = new List<XYZ>();
public class XYZ
{
public String TimeZone { get; set; }
public Decimal? B1Volume { get; set; }
public Decimal? B2Volume { get; set; }
public Decimal? B3Volume { get; set; }
public Decimal? B4Volume { get; set; }
public Decimal? B5Volume { get; set; }
// .............
// .............
public Decimal? B24Volume { get; set; }
public String Name {get;set;}
}
how do I select the properties B1Volume,........B24Volume ?
I tried with following code mentioned below, but it's not giving expected results
var hp = foo.Skip(1).Take(23).ToList();
There's a few ways, but I do not think that you want to go down that road.
Do you really want a list of xyz? Or asked in a different fashion: Do you have many different lists of lists of volumes? Or do you only want to express a single list of volumes?
Maybe what you want to do is declare an array inside XYZ like this
public class XYZ
{
public String TimeZone { get; set; }
public Decimal?[] Volumes {get; set;} = new Decimal?[24];
public String Name {get; set;}
}
If you want to access volumes by an index (1,2,...,24) you need an array or any other kind of indexed data structure.
Then you could do
var xyz = new XYZ();
xyz.Volumes[0] = 12.0;
xyz.Volumes[1] = 23.0;
.....
and basically access the volumes by xyz.Volumes and adding an index to get the n-th volume
If you now want to further list these XYZ you could do something like this:
var listOfXyz = new List<XYZ>();
listOfXyz.Add(new XYZ());
....
listOfXyz[3].Volumes
this would give you the 24 volumes of the element at the index of 3 in the list.
You need to do a Select:
var hp = foo.Select(x => new { x.BVolume1, x.BVolume2, ..., x.BVolume24 });
Although I do agree with #Himzo that this is not the best way to solve your problem if you can change the structure.
Maybe it helps:
XYZ xyz = new XYZ();
Type t = xyz.GetType();
List<PropertyInfo> properties = new List<PropertyInfo>(t.GetProperties());
var hp = properties.Skip(1).Take(23).ToList();
Do not forget adding name space:
using System.Reflection;
Update
In comments GBreen12 suggests to add a filter for getting only properties that has name containing volume. Now if you add another properties the code will not fail. So you can change the 3th line to this:
List<PropertyInfo> properties = (new List<PropertyInfo>(t.GetProperties())).Where(x => x.Name.EndsWith("Volume")).ToList();
Now you do not need last line var hp = ... and the properties is your answer.

Nesting list in model and the viewing and storing data

Sorry if this is a simple question but if I want to have a list inside a model, and later access and set the values of the list?
Say my main model looks like this:
public class StartPageModel : IPageViewModel<StartPage>
{
public IList<ListContent> ListContent { get; set; }
public StartPage CurrentPage { get; set; }
}
public class ListContent
{
public IList<ListElement> ArticleListContent { get; set; }
public IList<ListElement> InsightListContent { get; set; }
}
How can I set the ArticleListContent list to a value by referencing the parent model?
public ActionResult Index(StartPage currentPage)
{
var model = new StartPageModel(currentPage);
model.ListContent.ArticleListContent = GetListContent(currentPage.ArticleCollection);
}
However this returns the error:
IList does not contain a definition for 'ArticleListContent'
I'm not sure you require a collection of ListContent in your StartPageModel, correct me if I'm wrong.
Change
public IList<ListContent> ListContent { get; set; }
to
public ListContent ListContent { get; set; }
And provided ListContent is initialized, your assignment will work.
It's because it's referencing the List of ListContent, not an individual item in that list. Here's some examples:
var model = new StartPageModel(currentPage);
model.ListContent[0].ArticleListContent = GetListContent(currentPage.ArticleCollection); // Access first in list
model.ListContent[1].ArticleListContent = GetListContent(currentPage.ArticleCollection); // Access secondin list
model.ListContent.First().ArticleListContent = GetListContent(currentPage.ArticleCollection); // Access first in list using Linq

an array list with n rows and four columns in c#

I need a clear example that shows me how to define a list that has n rows and 4 columns and how to use it. I need a list to save my data like the below image. as you see this could be a dictionary.
You need to create a class with all the above properties
public class Sample
{
public string vocabulary { get; set; }
public string meaning { get; set; }
public int number { get; set; }
public int group { get; set; }
}
and then you can create a List of type Sample,
List<Sample> yourList = new List<Sample>();
You can add items to the list as below
yourList.Add(new Sample { vocabulary = "massive", meaning = "very big", number = 5, group = 15 });
You can access them later like this, if you want the first element,
var result = yourList[0];
this is the easiest and best way of doing it. You need to create a new class and then create new instances of the class and then add it to the list and then use LINQ to get the data out
void Main()
{
var list = new List<myClass>()
list.Add(new myClass() {
Vocabluary = "Vocabluary ",
Meaning = "meaning",
Number = 1,
Group = 2})
}
public class myClass
{
public string Vocabluary { get; set; }
public string Meaning { get; set; }
public int Number { get; set; }
public int Group { get; set; }
}
yes... as Sajeetharan mentioned, with a custom class you can create an any dimensions List. but i don't think you need to think about dimension in C#... it is a bit more high level than that.
just simply create a class and put everything you need in it...
public class CustomClass{
public string d1;
public int d2;
public string d3;
public string d4;
...
//you can easily create a N dimension class
}
to access it and apply it
public void Main(){
List<CustomClass> list = new List<CustomClass>();
CustomClass cc = new CustomClass();
cc.d1 = "v1";
cc.d2 = 0; //v2
list.Add(cc);
//to access it
foreach(CustomClass tmpClass in list)
{
string d1Value = tmpClass.d1;
int d2Value = tmpClass.d2;
}
}

DataValue field value 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";

Categories