473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

changing values in a column on a databound datagrid

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!
Nov 16 '05 #1
6 1959
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"spmm_pls" <sp******@yahoo.com> wrote in message
news:f5**************************@posting.google.c om...
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!

Nov 16 '05 #2
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"spmm_pls" <sp******@yahoo.com> wrote in message
news:f5**************************@posting.google.c om...
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!

Nov 16 '05 #3
OK, I'll adjust my SP. Thanks to both. I might have a look at the
color thingy later on.
Nov 16 '05 #4
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??
Nov 16 '05 #5
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"spmm_pls" <sp******@yahoo.com> wrote in message
news:f5*************************@posting.google.co m...
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??

Nov 16 '05 #6
Hi,

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

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"spmm_pls" <sp******@yahoo.com> wrote in message
news:f5*************************@posting.google.co m...
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??

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PeterZ | last post by:
Hi, In the past I've successfully databound a conventional Winforms checkbox to a number column in an Oracle table, where 1 represents true and 0 represents false. The checkbox would be ticked...
3
by: Doug | last post by:
Hi I have the following code (not mine) that populates a datagrid with some file names. But I want to replace the datagrid with a combo box. private void OnCurrentDataCellChanged(object sender,...
2
by: damonf | last post by:
I'm currently trying to add an ASP hyperlink to a template column in a datagrid. The normal hyperlink column doesn't give me the ability to add attributes to the item. In my grid there are four...
15
by: John Blair | last post by:
Hi, Code attached but the line that gives me an error is MyDataGrid.Columns(2).Visible = False It actually gives me an error for any value instead of 2 even when 9 bound columns of data exist....
1
by: Ken Varn | last post by:
I have a problem where my DataGrid would not maintain the ViewState of my databound rows. I finally narrowed down the problem. If my first column is a template column, the view state for the...
4
by: velu | last post by:
I have created a table MSSQL that contain values and published into Datagrid. The table is typically like this.. Tbl Article Rating Count 1 3 3 2 0 0 3 2 1 4 4 5
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
1
by: Mike | last post by:
Hello all, Is there a way to change the ItemTemplate based on the data that is being shown? For instance, I have a dataset, contains two fields (NAME, STATE). I want to show all the records...
2
by: KerryL | last post by:
How do I update the value of a databound column within my datagrid from within the SelectedIndexChanged event of a dropdownlist, also found within my datagrid? I believe I need to determine the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.