0

So I have a datagrid. When a row is doubleclicked the value is passed to the MVVM.

List Implementation:

private List<Object> _AllQueries { get; set; }
public List<Object> AllQueries { get { return _AllQueries; } set { _AllQueries = value; this.NotifyOfPropertyChange(() => AllQueries); } }

Datagrid Implementation:

<telerik:RadGridView SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding AllQueries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True">

Implementation of SelectedItem

public Object SelectedItem { get; set; }

When I put a breakpoint where SelectedItem is used it's working perfectly, the values are being brought. But my question is How do i interact with those Columns or Properties

using (var ctx = DB.Get()) i = ctx.Interactions.Find(SelectedItem2);

Examples of what SelectedItem would be:

Id = 200
Date = 4/24/2014
Name = "Billy Bob"

Is there a way to index through the properties of an object something like SelectedItem[0] would give me the Id Number.

Master
  • 2,038
  • 2
  • 27
  • 77
  • Why do you have a list of `object` in the first place? You should have a list of your actual object's definition, so that you can then access the actual fields from it. – Servy Apr 24 '14 at 16:05
  • There's not an object definition because the objects are ambiguous. They come with different values, one iteration it might be date name, next is subject and Date – Master Apr 24 '14 at 16:06
  • How do you intend to meaningfully use the data then if you don't know what data is there? – Servy Apr 24 '14 at 16:07
  • The First Column is always ID but the other columns are always ambiguous, I dont need to know anything else. – Master Apr 24 '14 at 16:08
  • Then all of the objects should implement an interface that defines an Id member. – Servy Apr 24 '14 at 16:09

2 Answers2

1

Given this comment of yours:

The First Column is always ID but the other columns are always ambiguous, I dont need to know anything else.

You just need a way of accessing the IDs of a list of objects in which each object has an ID property. You should use an interface to do this. You should ensure that each object in your queries results implements an interface that defines an ID property, and ensure all of the query's results implement that interface. Change the list to be a list of that interface, rather than a list of objects.

Community
  • 1
  • 1
Servy
  • 202,030
  • 26
  • 332
  • 449
  • The only problem with this implementation is that it may display other "columns" that I won't need. So if I make an interface class called AllQueryClass, which has all properties and is binding to AllQueryClass it'll display the extra info with blanks if they aren't used. I considered this method. – Master Apr 24 '14 at 16:15
  • @user3276954 I in no way told you to create such a type. I told you to create an interface that has nothing but an ID property, to which all of the various objects representing all of the possible queries will implement. – Servy Apr 24 '14 at 16:17
  • I don't see why on earth I would have to use this method, clearly the question was asking how to iterate through object properties. Answered below "Using Reflection" Thank for your input definitely allowed me to view it at different angles – Master Apr 24 '14 at 16:41
  • @user3276954 This is the appropriate way of solving the problem for a well designed solution. The situation you are describing is the exact situation that interfaces are *designed* to solve. Reflection is a tool that is not well suited for this task. – Servy Apr 24 '14 at 16:46
  • Could you possibly forward me this "Interface" method, my options are still open. If I could do a bit more research on it and get more knowledge maybe i'll sway over – Master Apr 24 '14 at 16:50
  • @user3276954 Do you know how to create an interface? You create one, and you give it an ID property. You create a list of that interface. You can now access the ID of any object in that list. Volia. – Servy Apr 24 '14 at 16:51
-1

For a generic object coming back, you'd want to try Reflection to see what properties are there, then grab the value of the ones you want.

EDIT: Initially misunderstood the question.

Community
  • 1
  • 1
gfish3000
  • 1,557
  • 1
  • 11
  • 22
  • @user3276954 what does "doesn't work" mean? Is there an error? Does it return the wrong thing? – gfish3000 Apr 24 '14 at 16:06
  • You cannot Index through an object like that. Please try it first – Master Apr 24 '14 at 16:07
  • @user3276954, you cannot index through an object, period so your use of the said term is misleading. What you can do is look into it and build one through which you can iterate by adopting the method in my now edited answer. – gfish3000 Apr 24 '14 at 16:12
  • Sorry if you misunderstood the question. – Master Apr 24 '14 at 16:15
  • @user3276954, it happens. Just use the code in the answer for the link, just instead of writing out the results to the console, include them in a `Dictionary` called, say `ObjectMap` and then get your `Id` property with something like `var id = (int) ObjectMap["Id"]` or whatever type you're expecting your id to be. – gfish3000 Apr 24 '14 at 16:18