|
|
Comments and Discussions
|
|
 |

|
Hi,
I'm using following statement to retrieve a value from a cell(datalistview):
this.dlvData.Items[this.dlvData.SelectedIndex].SubItems[1].Text;
This works perfectly but I would like to call the cell value by key, like this:
this.dlvData.Items[this.dlvData.SelectedIndex].SubItems["olvColumn01"].Text;
This gives null, any idea how to call the cell value by key/name instead of using the index.
Thanks in advance for the support.
Benjamin
|
|
|
|

|
SubItems["string"] gets the value of the subitem with a string key, not necessarily the value of any particular column.
In general, you should not be using (or even need to use) regular ListView methods & properties, as these are not guaranteed to be in sync with the ObjectListView internals. Use ObjectListView methods instead, such as olv.Objects intead of olv.Items and olv.SelectedObjects instead of olv.SelectedItems.
Since your OLV should be using model objects, why not simply ask the object what its value is instead of prodding the view directly?
e.g. instead of
this.dlvData.Items[this.dlvData.SelectedIndex].SubItems["olvColumn01"].Text;
use something like
MyModel model = (MyModel)dlvData.SelectedObject;
string text = model.FirstName;
|
|
|
|

|
Hello,
first of all thank you for your reply.
this.dlvData.Items[this.dlvData.SelectedIndex].SubItems["olvColumn01"].Text;
This code never works for me, that's my question on the first place, if this would work, my problem was solved. => Always returns null for the moment.
I 've always used instead of the key the index and it has never failed me. But the problem is when you add a new column => you can start to count all over => is not a good solution.
The method you mention is new to me and I don't get it to work.
What exactly is Mymodel in your example? Could you please give me an example with my parameters(see below)?
I'll try to explain more what I want to do:
I have a datalistview with property fillrowselect = true and no multiple selections allowed. The user can click a row and click on a button for example. Then the idea is to retrieve the values from the selected row and do some things with it.
So I have: dlvData is my datalistview
olvColumnName is a column with string content
olvColumnActive is a column with a boolean content
so how can I use your method to do this:
string strName = this.dlvData.Items[this.dlvData.SelectedIndex].SubItems[1].Text;
boolean blnActive = Convert.ToBoolean(this.dlvData.Items[this.dlvData.SelectedIndex].SubItems[2].Text);
Thanks in advance for your help.
Best regards,
Benny
|
|
|
|

|
I'll admit that I don't have much experience using OLV's DataListView, but the methodology should be the same. You use an OLV to display a view of some model object and its properties.
Remember, dlv.Items is a regular ListView member and it is not guaranteed to do exactly what you want. Instead, use an ObjectListView member, such as "SelectedObject".
DataRowView row = (DataRowView)dlv.SelectedObject;
string name = (string)row["Name"];
bool isActive = (bool)row["IsActive"];
How are you populating your list? It's possible that DataListView is not the most appropriate type of OLV for your situation. For example, using a regular ObjectListView, which you populate with objects of a class MyModel that you specify, the above could become:
MyModel model = (MyModel)olv.SelectedObject;
string name = model.Name;
bool isActive = model.IsActive;
Use whatever makes sense to you, but one of the goals of OLV is to do a lot of the work for you so that you don't HAVE to poke and prod directly at the ListView or its ListViewItems and ListViewSubItems.
|
|
|
|

|
Thank you very much for your help. This was exactly what I wanted to achieve.
Much better then my method.
I've learned a lot from your explanation and example here.
Best regards,
Benjamin
|
|
|
|

|
This control has helped me a great deal, so I'm pleased to help others with it when I can
|
|
|
|

|
I have been chasing a bug for several days that gives me; "...is not a parameter-less method, property.." as text in my subitem. I thought this was a C# error but found nothing on Google, so searching for the text found it in OLV Munger.cs. It seems that on a certain OLV column that if I use an AspectName I get this error instead of the text that should be in that field. If I remove the AspectName then I just get a blank field, instead of my data. There does not seem to be anything unique about the column, but it will not show the data (that is confirmed to pass into OLV), where all the other columns work as designed.
Anyone got any suggestions?
Thanks
modified 24 Apr '13 - 7:29.
|
|
|
|

|
If you get the "...is not a parameter-less method, property or field of type..." warning, then it's exactly that--the Aspect you've used does not exist in your model object. e.g. if your Aspect is "FirstName", then make sure your model object contains a "FirstName" property, field, or parameterless method, i.e. model.FirstName. Remember also that this is case sentitive. If your Aspect is deeper, e.g. for a "car" model, Engine.Oil.IsEmpty, make sure every entity along the way exists. In any case, there must be something very subtly wrong in your aspect name.
|
|
|
|

|
Thanks for that feedback. As it turns out i'm not using Aspects at all. I put a name in the properties field because I discovered that if I left that property field empty no data would be printed into sub- items, so I added a simple unique name in each column, but don't use it.
Just tried removing All aspect names, but just got an (null) printed to control and nothing else.
this bug has got me stumped.
thanks
|
|
|
|

|
If you're using an ObjectListView, then you probably should be using aspect names on each of your columns. This is the OLV's flagship way of automagically putting the value of your model objects' properties, etc. into the columns/cells of the list view. You can do this from the Designer or from code, but if your columns are static and you created them in the designer, then you probably should put the aspect name in there at the same time.
public class MyModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public MyModel(string first, string last)
{
this.FirstName = first;
this.LastName = last;
}
}
public void SomeInitializationMethod()
{
FirstNameColumn.AspectName = "FirstName";
LastNameColumn.AspectName = "LastName";
}
public void AddItems()
{
MyModel model1 = new Model("John", "Smith");
MyModel model2 = new Model("Jane", "Doe");
myOlv.AddObject(model1);
myOlv.AddObject(model2);
}
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
An easier to use ListView that supports sorting, grouping, editing, overlays, and drag-n-drop.
| Type | Article |
| Licence | GPL3 |
| First Posted | 17 Oct 2006 |
| Views | 2,558,796 |
| Downloads | 77 |
| Bookmarked | 1,540 times |
|
|