GridView - how do i modify bound data column by column?

T

TS

i have a custom object list for my data source. i want to be able to
override some method so that whenever a column in row is bound, i can take
that value that gets bound, which is an integer, and decode that into it's
code table's textual value.

so if i have a collection of Car objects who have a property called Color,
which is an integer that points to a table that i can covert to its text
form:

ColorTable
1 = 'blue'
2 = 'red'

Where can i go to do this? I figured if i override OnDataBound, i could loop
thru all the rows and then thru all the columns and convert the values then,
but that means i have to do a looping operation - i figured i should be able
to do it as it gets bound???

thanks
 
T

Teemu Keiski

If you handle RowDataBound event, it is automatically called for every row
in the data source, so you don't need to loop rows since it's already
provided to you by handling the event. Only thing to do then is tyo deal
with the columns in the data source. If they are fixed, you could do it
fixed column by column and you don't have to loop.
 
W

Walter Wang [MSFT]

Hi TS,

There're two approaches for your objective. One is mentioned by Teemu:
change the data after each row is bound.

Another approach is to directly convert the data to your desired one using
a code-behind function and do this in the declarative markup with a
TemplateField

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblColor" runat="server"
BackColor=<%#
System.Drawing.Color.FromName(GetColor(DataBinder.Eval(Container.DataItem,
"ColorIndex")))%> Text=<%# GetColor(DataBinder.Eval(Container.DataItem,
"ColorIndex")) %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>


protected string GetColor(object colorIndex)
{
switch ((int) colorIndex) {
case 1:
return "blue";
case 2:
return "red";
case 3:
return "yellow";
case 4:
return "purple";
}

return "white";
}



Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi TS,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top