By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,630 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,630 IT Pros & Developers. It's quick & easy.

Determining Object Passed to a Templated Column

P: n/a
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated columns work find as long as I pass in a dataset but if I pass in a collection of objects then they won't work unless I can cast the correct object and then fill in the column with the object. My current code looks like this:

public void BindData(object sender, EventArgs e) {
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
Folder folder = (Folder) (((DictionaryEntry) container.DataItem).Value);
l.Text = folder[_column];
}

and it works fine as long as I know the object. But I can't figure out how to get it to work in a more generic manner - IE: I want to be able to determine the object on the fly, cast it and then have the BindData method populate the column from the _column in the object without knowing what the object is in advance.

Anyone have any pointers they'd like to share to get me in the right direction?

Thanks,

Alex

Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

I think you arn't in the right direction. check out :
1)
http://www.dotnetjunkies.com/Tutoria...863-F85EE1AA76
2C.dcik
Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2

P: n/a
Actually, I was being pretty dense, just adding the following solved it

object item = (object)(((DictionaryEntry) container.DataItem).Value);
DataBinder.Eval(item, this._idfield)

As long as I pass in an object then its no problem - I think I need to test to see whether its an object or a datarow so that I could handle both but this works.

Alex

"Alex" wrote:
That's actually the approach I started to take but I wind up with a lot of if then constructs trying to determine what the appropriate object is. I'd like this to be more generic so I can decouple the object type that is passed in from retrieving the result. So I came up with the following which just casts a generic object:

object item = (object) (((DictionaryEntry) container.DataItem).Value);
l.Text = item[_columnname];

The only problem with this is that the compiler won't compile it because I get an error that states I can't apply indexing to a type Object.

Alex

"Natty Gur" wrote:
Hi,

I think you arn't in the right direction. check out :
1)
http://www.dotnetjunkies.com/Tutoria...863-F85EE1AA76
2C.dcik
Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #3

P: n/a
Actually, I was being pretty dense, just adding the following solved it

object item = (object)(((DictionaryEntry) container.DataItem).Value);
DataBinder.Eval(item, this._idfield)

As long as I pass in an object then its no problem - I think I need to test to see whether its an object or a datarow so that I could handle both but this works.

Alex

"Alex" wrote:
That's actually the approach I started to take but I wind up with a lot of if then constructs trying to determine what the appropriate object is. I'd like this to be more generic so I can decouple the object type that is passed in from retrieving the result. So I came up with the following which just casts a generic object:

object item = (object) (((DictionaryEntry) container.DataItem).Value);
l.Text = item[_columnname];

The only problem with this is that the compiler won't compile it because I get an error that states I can't apply indexing to a type Object.

Alex

"Natty Gur" wrote:
Hi,

I think you arn't in the right direction. check out :
1)
http://www.dotnetjunkies.com/Tutoria...863-F85EE1AA76
2C.dcik
Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #4

P: n/a
"Alex" <Al**@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping
someone here could jumpstart me. My templated columns work find as long as
I pass in a dataset but if I pass in a collection of objects then they won't
work unless I can cast the correct object and then fill in the column with
the object. My current code looks like this:
public void BindData(object sender, EventArgs e) {
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
Folder folder = (Folder) (((DictionaryEntry) container.DataItem).Value); l.Text = folder[_column];
}

and it works fine as long as I know the object. But I can't figure out how to get it to work in a more generic manner - IE: I want to be able to
determine the object on the fly, cast it and then have the BindData method
populate the column from the _column in the object without knowing what the
object is in advance.
Anyone have any pointers they'd like to share to get me in the right

direction?

If you don't know what the object is, how would you know what the "value"
is? The object could have two "values".
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #5

P: n/a
When I initialize the templated control, I pass in the name of the value I want to render. In this way I can pass in a collection of objects. The collection will always contain the same type object but different collections may have different objects. I am trying to get my templated control to handle an object in the same way it would handle a dataset.

Alex

"John Saunders" wrote:
"Alex" <Al**@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
I have been loving the templated datacolumns of the datagrid. I have

stumbled onto a problem though that is beyond by knowledge and I was hoping
someone here could jumpstart me. My templated columns work find as long as
I pass in a dataset but if I pass in a collection of objects then they won't
work unless I can cast the correct object and then fill in the column with
the object. My current code looks like this:

public void BindData(object sender, EventArgs e) {
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
Folder folder = (Folder) (((DictionaryEntry)

container.DataItem).Value);
l.Text = folder[_column];
}

and it works fine as long as I know the object. But I can't figure out

how to get it to work in a more generic manner - IE: I want to be able to
determine the object on the fly, cast it and then have the BindData method
populate the column from the _column in the object without knowing what the
object is in advance.

Anyone have any pointers they'd like to share to get me in the right

direction?

If you don't know what the object is, how would you know what the "value"
is? The object could have two "values".
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #6

P: n/a
"Alex" <Al**@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
When I initialize the templated control, I pass in the name of the value I

want to render. In this way I can pass in a collection of objects. The
collection will always contain the same type object but different
collections may have different objects. I am trying to get my templated
control to handle an object in the same way it would handle a dataset.

Well, you can look into using Reflection. Read up on the PropertyDescriptor
class, TypeDescriptor class and Type class.
--
John Saunders
johnwsaundersiii at hotmail

"Alex" <Al**@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
I have been loving the templated datacolumns of the datagrid. I have

stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated columns work find as long as I pass in a dataset but if I pass in a collection of objects then they won't work unless I can cast the correct object and then fill in the column with the object. My current code looks like this:

public void BindData(object sender, EventArgs e) {
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
Folder folder = (Folder) (((DictionaryEntry)

container.DataItem).Value);
l.Text = folder[_column];
}

and it works fine as long as I know the object. But I can't figure
out how to get it to work in a more generic manner - IE: I want to be able to determine the object on the fly, cast it and then have the BindData method populate the column from the _column in the object without knowing what the object is in advance.

Anyone have any pointers they'd like to share to get me in the right

direction?

If you don't know what the object is, how would you know what the "value" is? The object could have two "values".
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.