How to grab value of selected objects in ObjectListView - c#

I have an objectlistview which displays filename and its path in a column. I would like to run a function on selected items. Is there any way I can grab the value of the filename and loop through every file that is selected on the objectlistview? The column aspect name is Filename.
My function is as follows: sampleFunction(string inputFile, string outputFile);
so far I've tried this, but couldn't work, I know I'm missing reference to the column itself but I don;t know how to add it:
for(var i=0; i<=objectListView1.SelectedObjects.Count; i++)
{
encClass.sampleFunction(objectListView1.SelectedObjects[i], "output here");
}
edit:
I also tried append ToString() method to objectListView1.SelectedObjects[i].ToString(). It shows no error but the function couldn't run perfectly because I have 3 columns and I only wanted to use the first column's value in the function as the inputFile value.

I seems that you do not understand the concept of the OLV correctly. I suggest you read the tutorial (again).
The OLV allows you to work with the underlying model objects directly, so just cast the selected object(s) to the original type and access its properties. You can even use a TypedObjectListView<>, which simplifies access to the models.
Judging from you post, what you want to do is probably something like this:
foreach (var selectedObject in objectListView1.SelectedObjects) {
encClass.sampleFunction(((MyType)selectedObject).Filename, "output here");
}
Obviously, replace "MyType" with your model object type.

Related

Is it possible to use a string value to specify the name of a parameter's string accessor?

I'm feeling like I'm missing something very important from my past experiences, however, here it is:
I am designing an end-user report designer using DevExpress for clients within my corporation. Since these are being generated dynamically, I would like to give clients the opportunity to have free range over what they are putting on the report. Instead of hard-coding all of the available values, I'm using a StartsWith and a Substring to retrieve the name of the control. I would like to use this string value to specify which parameter field they would need. I'm not sure if I'm thinking about this correctly or if I'm way off track, hoping someone can take another look and give me some suggestions.
Any information will be appreciated.
Maybe I need a dictionary or some sort of interface? Code snippet of what I currently have is below:
private static void BuildReportHeader(Notice notice, XRControl control)
{
if (control.Name.StartsWith("lbl"))
{
string elementName = control.Name.Substring(3);
XRLabel label = (XRLabel)control;
label.Text = notice.{elementName};
}
}
Obviously, this doesn't work or compile as it currently sits, just looking for a solution to the {elementName} bit so that I can access any field on that specific parameter.
You can use reflection to perform this dynamic value lookup: notice.{elementName}.
The bellow sample codes assumed that elementName exist and it's public.
If elementName is a property
var propertyValue = typeof(Notice).GetProperty(elementName).GetValue(notice);
Please bear in mind that GetValue returns an object
The above code assumed that the elementName property has a getter
If it does not have then you will receive an ArgumentException
If elementName is a field
var propertyValue = typeof(Notice).GetField(elementName).GetValue(notice);
The above code assumed that the elementName points to a field
If you provide a non-existing field name or a property/method name then you would receive a NullReferenceException because GetField would return null

How can I iterate through an Entity Framework Table or Type that holds columns you normally dot into?

So I'm using Entity Framework 6 and what I want to do is simply loop through the columns inside the model below instead of manually dotting into each column to set a value. I've tried googling a solution but I can't find one that does what I want. I get an error that the Type doesn't have a GetEnumerator() available. I have no idea how to proceed but don't want to give up without first asking. Any advice would be great, thank you everyone.
CaseManagementExtractedEntity addRecord = new CaseManagementExtractedEntity();
foreach(var item in addRecord)
{
item = someValue;
}
context.CaseManagementExtractedEntity.Add(addRecord);
context.SaveChanges();
If I understand your question, then you have to use reflection to
get all the properties of addRecord like this:
var listOfProps = addRecord.GetType().GetProperties();
then loop through this list and assign the value you want to each property using the SetValue() method.
Look here for more info on how to set the property values with reflection

Get Property Value From Nested Object List

I am not sure how to even title or ask this question, so apologies for any confusion.
I need to get the value of the Id from the first list in Model.Content.GetPropertyValue("SlidePanel"). I've tried many, many, many thing with my troubles usually being that "You can't do this because it's an Object". In the image below, I want to get the Id Value of "1092" as a String.
-----EDIT-------
I was able to get the value with the code below. Casted the list, grabbed the first list since it would always be that option (I wrapped it in an if, but removed it in this example), then I was able to specify the property I needed and converted as needed.
If I sound like I don't speak this language fluently, it's because I'm still fresh to development. Thanks for everyone who helped.
dynamic slidePanelObject = Model.Content.GetProperty("SlidePanel").Value;
List<object> slidePanelCast = ((IEnumerable<object>)slidePanelObject).Cast<object>().ToList();
dynamic slidePanelFirst = slidePanelCast.First();
var slidePanelId = slidePanelFirst.Id;
string slidePanelString = slidePanelId.ToString();
Model.Content.GetPropertyValue is probably returning an System.Object, so you'll need to cast your temp var to a List<T> type of some kind before you can access it like a list.
Without knowing all the types involved, here's some code that you could modify:
var temp = Model.Content.GetPropertyValue("SlidePanel") as List<TYourType>;

WPF ListView places garbage into a log file

When i try to save my list view lines into a log file using StreamWriter:
sw.WriteLine(listview.Items[0]);
it places to the beginning of the line name of LIstViewItem class, and my log looks like:
System.Windows.Controls.ListViewItem: text text text;
I tried to use:
sw.WriteLine(listView.Items[0].ToString());
and:
sw.WriteLine(listView.Items[0].ToString() as string);
but nothing helpes:(
What am i doing wrong?
The ListView.Items property is an ItemCollection. The items contained are typed as Object. You must cast the item to whatever type you populated the ListView with and then access the appropriate property from there. See: https://msdn.microsoft.com/en-us/library/ms605127(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items(v=vs.110).aspx for more information.
By default, the console will use the ToString() method as the output. All of the methods you have tried will be using the ToString() method.
Perhaps you are looking for the Text property.
sw.WriteLine(((ListViewItem)listview.Items[0]).Text);

Understanding how a property of type List<> works

I got to think I don't understand how it works.
My specific question is: Why am I allowed to set the value of a list property element when I have no setter and no backing list variable?
Let me explain. Let's say I have a CustomerTable class with:
public List<string> Name
{
get
{
var names = new List<string>();
foreach (CustomerRow row in Rows)
{
name.Add(row.Name);
}
return names;
}
}
The idea is to have a read-only property show the contents of a column without duplicating data in my class, since I already have a list of rows.
Anyway, my surprise comes when pieces of code like the following one are accepted by Visual Studio without claiming any kind of error (and it even allows me to compile without errors):
Name[0] = "John";
I can't understand why this is legal. My property has no set { }, and it doesn't even have a backing list to modify. What is this piece of code supposed to do?
Shouldn't it work like a method? Is there really a stored list other than the one I generate each time someone "gets" it?
(I can give more details on demand and will also be grateful for any other remarks)
You are not setting the property, rather you are getting the property (which is a list) and then operating on it (in your example, changing its first member). If you were to try:
Name = new List<string>();
You would get the compilation error you were expecting to get. Note that since you are creating a new list every time, your Rows property remains read-only (assuming it is not exposed somewhere else). If you want to make it clear that changes to your returned collection are meaningless, you can change the type of the Name property to IEnumerable<string>:
public IEnumerable<string> Name
{
get
{
return Rows.Select(row => row.name); //LINQ is more elegant here
}
}
In your example you don't set Name (which is read-only, indeed), but you set the first list element contained in Name, which is Name[0], and there's no reason why you could not do that since List<string> is an object type which allows to set elements.

Categories

Resources