tags:

views:

55

answers:

5

Markup:

<asp:GridView ID="GridView1" runat="Server">
   <Columns>
      <asp:BoundField DataField="status_column" HeaderText="Status" />
      <asp:BoundField ...
      <asp:BoundField ...
   </Columns>
</asp:GridView>

Code:

GridView1.DataSource = _dataSet
DataBind()

The values stored in my database are integers, 1 - 9. Each value has an associated string value that I want displayed in my GridView. Ex: 1 = "Active"; 2 = "On Hold"; etc. How would I filter the incoming data so I can display the associated String value instead of the Integers from my table?

+1  A: 

instead of doing it in C# , If you are using SQL you can try with

when then condition in a query

SELECT title, price,
        Category = CASE column1
         WHEN column1 = '1' THEN 'Active'
          WHEN column1 = '2' THEN 'On Hold'
          .....
          ELSE 'your value'
        END,
FROM table

which then bind to the gridview

anishmarokey
What's "Category"?
category is an alias name. this will come in .cs page to bind
anishmarokey
+2  A: 

You have two options:

  1. Modify your DataSet, so that it will containt a column for the string representation of the data you want to show.
  2. Use a TemplateField containing a Label. Subscribe to the OnRowDataBound Event of the GridView, and get that Label with e.Row.FindControl("LabelID"), cast the e.Row.DataItem to your DataRow, and assign the string representation of your integer value there.

I would advise against the solution posted by anishmarokey, because the query is not the correct place for this. Think a little big bigger, about localization for instance. Wouldn't work with that solution, but with both of my suggested solutions.

Edit1: Similiar answeres from me regarding the OnRowDataBound Event. If these don't help you, I'm afraid you need to use google to find more examples:
http://stackoverflow.com/questions/2272015/programmatically-access-gridview-columns-and-manipulate/2272058#2272058
http://stackoverflow.com/questions/2458351/custom-gridview-delete-button/2458978#2458978
http://stackoverflow.com/questions/2985554/how-to-bind-a-complex-dictionary-to-a-gridview-in-asp-net/2985993#2985993

citronas
could you possibly show an example of your two solutions? thanks.
I added some code related examples for the second option
citronas
A: 

you can save a list of KeyPairValue using Dictionary and try to get the value in the View:

Dictionary<int,string> stringList = //Any code to fill the dictionary, now each integer is related to a string.

in the page

 <asp:BoundField DataField="<%Eval(stringList[status_column])%>" HeaderText="Status" />
Nour Sabouny
Have you tried the code? Does that work? I don't have an IDE right here, but shouldn't it look like this: '<% this.stringList[Eval("status_column")]%>', or '<% this.stringList[Eval("status_column")];%>'
citronas
ooops, sorry it's my fault, i didn't try the code, but i wanted the idea to be clear. citronas, i think your code is correct.
Nour Sabouny
+1  A: 

Assuming you are not editing in the gridview, I would just join to the FK table in the SQL and return that as the status instead of the ID.

--Include this as part of your select statement for the datagrid:
SELECT CategoryText as status_column
FROM status_table st INNER JOIN category_table ct ON st.status_column = ct.status_column
Decker97
+1  A: 

Since this is coming back from a database, I think that is is very likely that you will find at table that defines the categories.

All you have to do is join the two

Select Item.Id, Item.Title, Item.Price, Category.Id, Category.Name
From Item
   Inner Join Category
       ON Item.CategoryId = Category.Id
Raj More