0

In my database I represent fruit as 1 for apples, 2 for oranges, 3 for pears in a database field as an int.

My problem is when I output this using my GridView on the page it shows "1,2,3" so on when I want it to be displaying the fruits the numbers represent.

What I want is it to go if (fruit==1) display "Apple" etc.

Can I do this within the .aspx page?

5 Answers 5

1

You can use a TemplateField containing a Label. The steps are shown below:

  1. Add a TemplateField associated with fruit containing a Label.(e.g. lblFruit)
  2. Associate the OnRowDataBound Event of the GridView
  3. Implement event (C#) as follows:

    protected void gvFruits_RowDataBound(object sender, GridViewRowEventArgs e) {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblFruit = e.Row.FindControl("lblFruit") as Label;
        switch(value_from_database)
        {
           default:
           case 1:
             lblFruit.Text = "Apple";
           case 2:
             lblFruit.Text = "Orange";
           // ... 
        }
    }
    

    }

To get current record associated with row, you can use e.Row.DataItem property. A nice discussion is shown here (citronas' answer)

Sign up to request clarification or add additional context in comments.

2 Comments

That link does not work and I could not get your example to work. Where it says value from DB am I meant to place in eval("fruitField") ?
The link is share so link to an answer, but anyway here it is: stackoverflow.com/questions/3814798/…. value_from_database means that you need to get this value from your record. I do not know how you are binding the grid view, but citron'as answers provide good examples on how to do it.
1

The best thing to do in this scenario is to create a Enum type that will assign the numbers to Fruit Names.

enum Fruit
    {   
        Apple=1, // Assigned integer value = 1 to apple
        Guava=2,    
        Banana=3
    };

public Fruit FruitName
{
             get;   // Auto implemented prop. of type Enum
             set;  
}

In this way , you can compare database integer values with fruit name

switch(value_from_database)
       {
           default:
           case 1:
             return Fruit.Apple;
           case 2:
             return Fruit.Guava;
           // ... so-on
       }

Comments

0

I would recommend that your database contain a Fruit Table with ID and Name columns and you get the values from the Database.

Comments

0

In the table put a field with the name of the fruit.

And the Sql Consult might look like this.

Select fruit_name from fruits where id = (1 or 2 or 3)

1 Comment

I want to avoid doing any extra database tables or fields.
0

You can create a getter property in the clas that will decide what to show based on the value.. Something like this maybe:

public string FruitName
{
   get
   {
       switch(someOtherField)
       {
           default:
           case 1:
             return "Apple";
           case 2:
             return "Orange";
           // ... 
       }
   }
}

Then you can use this property instead to show it to the user.

That is of course, if your question was just an example of a mismatch between the values in the database and the values you wanted to show to the user. If that's not the case and it was an actual example of your app, you should definitely put more work into your database table design, relations etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.