changing values in a column on a databound datagrid

S

spmm_pls

Hi,

Could some1 please tell me what the best way is to adjust the value of
a column in my databound datagrid (in ASP.NET)

For example my database returns the values 1,2 and 3 in the column
Priority. But 1,2 and 3 actually represent "Low", "Medium" and "High".
So instead of a column filled with 1,2,3's I want a column filled with
Low,Medium,High's. How do I do that??

Can (should) this be done in my C# code, or do I have to adjust my
stored procedure on which the datagrid is based?

thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

spmm_pls,

Personally, I would change the stored procedure to return not only the
value in the enumeration, but the description as well. It would probably be
best for the database to do this, as it requires another join, but it is
definitely better than opening up another connection to get these values,
and better than looping through your result and assigning the items.

Hope this helps.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

As Nicholas suggested it's better if you can do it in the SP, now in the
case that the meaning of the values are not saved in the DB or just that you
want to display it someway different ; like a different color depending of
the priority ; you can use a code like this:

in the aspx page:

<asp:templatecolumn ItemStyle-VerticalAlign="Top"
ItemStyle-Width="70" ItemStyle-HorizontalAlign="left"
ItemStyle-Cssclass="sidebar2">
<itemtemplate>
<asp:Label CssClass='<%# SelectDaysLeftCssClass(
((Accident)Container.DataItem) ) %>' Runat="server" Text='<%#
((Accident)Container.DataItem).Status %>' ID="Label2">
</asp:Label>
</itemtemplate>
</asp:templatecolumn>

As you can see I use a method to return the correct CssClass of the label
depending of the element being binded.
BTW, I bind the grid to a strong typed collection, but its the same if a
datagrid/datatable is used.

later in the code behind :
protected string SelectDaysLeftCssClass( Accident accident)
{
//If closed get the "blue" one
if ( accident.Status == Accident.AccidentStatus.Closed )
return "textblueReal";
//The record is open, if overdue red, normal otherwise
if ( accident.DaysLeft > -1 )
return "text";
else
return "textred";

}

The same thing you can use to display an interpretation of a value from the
database.

Hope this help,
 
S

spmm_pls

OK, I'll adjust my SP. Thanks to both. I might have a look at the
color thingy later on.
 
S

spmm_pls

Oh, by the way, the meaning of the values indeed is not stored in the
db. I adjusted the SP like this: SELECT CASE Priority WHEN 1 THEN
'Low' WHEN 2 THEN 'Medium' etc..

Is this a good idea??
 
N

Nicholas Paldino [.NET/C# MVP]

spmm_pls,

I don't think that this is a good idea. Personally, I think that you
should have another table which has the value linked up with the name. This
way, you are not bound to your stored procedure in order to generate those
values, should you need them somewhere else.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

If this is the case then you better use my suggestion, or just add another
table as paldino suggest.

Cheers,
 

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

Top