Saturday, March 31, 2012

Simple "Container..." question

I've been wokring with .NET for a few weeks now, but have been struggling to understand why I can't write the following: Container.DatatItem("a_column_name")

I've seen it in lots of examples but can never use this function myself as it always brings up an error that this denotes a property where a method was expected

Any help will be appreciatedWhat language are you using? If C#,try it with [] instead of ().
i have been using C# and it usually only works by using DataBinder.Eval...
I'm sorry, but I don't understand what you mean. Can you post the faulty code and the error message?
well it's not really faulty code, but when i see in example code the line Container.DataItem("some_column") i try to copy it into my own code and always fails.

Yet if I write the following, it seems to do the same job as I believe that the Conatainer.DataItem("some_column") is meant to do:

DataBinder.Eval(Container.Dataitem, "some_column);

Am I right in saying that the code above does exactly the same as Container.DataItem("some_column")?
Well, no, as Container.DataItem("some_column") fails...
Did you try Container.DataItem["some_column"] ?
Yep, I tried that, and it brings up an error saying

"Cannot apply indexing with [] to an expression of type 'object'"
If your dataitems are of type foo, do this:
((foo)Container.DataItem)["some_column"]
Just tried that, and it doesn't seem to like the arguements that I've given to the cast.

For example I tried casting it to a string and it gave the error:

The best overloaded method match for 'string.this[int]' has some invalid arguments
Not to add my 2 cents or anything, but dodgster02, you're not going nuts, I have never had any luck using Container.DataItem with any kind of indexer ([ or otherwise... I am self-taught and have seen the same examples over and over again, but when I use those examples I put them in and they break... even if I copy and paste them...

I always resolved myself that it was just a case of the examples using a beta accessor that was removed for the release or whatever... Either way, the difference if you can use one over the other is pretty big...

As I understand it, Container.DataItem() is early bound, whereas DataBinder.Eval(Container.DataItem, "ColName") is late bound... Which as anyone will tell you, Late bound is typically a HUGE performance hit.

Anyway... enough of my ramblings... maybe someone can shed some light on this or solve our dilemma...

Ciao
mcse 3010 is right about the performance hit of Eval (even though it it not HUGE, it exists). It currently uses reflexion to get to the right property. The advantage is that the page designer doesn't have to know the type of the data container to build the binding code.

So using Container.DataItem to get to the data is certainly more efficient, but it needs the kind of information that the designer doesn't have, that is a good knowledge of the real type of Container.DataItem.
In your particular case, what is it exactly? Is it really a string? I doubt it, and I suspect that what you're trying to do is something more along these lines:
(string)(((SomeCollectionType)Container.DataItem)[0])

Hope this helps.
well what I think i'm doing (haven't been programming in .NET for long) is that i'm trying to pull out the value from the specified DataGrid and the only way it's currently working is by using the DataBinder.Eval method.

I'm not completely sure whether i'm right but i know that the value that i want would be a string, and i know that it automatically converts this value into a string to be used by a function

0 comments:

Post a Comment