Friday, May 11, 2007

Display image in GridView according to the value

Scenario:
I have a boolean field in the dataset called "isImportant".
I want to bind this field to the GridView, so that if the value is true, it'll show a red flag.
Display Image in GridView

Solution:
I actually searched for quite sometime, and at last found that it's actually very simple to do that.
1. Create a Template Field for the column.
2. Add a Image control in the ItemTemplate.
3. Set the ImageUrl for the Image.
4. Right click on the Image control and select "Edit DataBindings....".
5. You'll see the "DataBinding" dialog box.
Edit databinding
(Click to see the larger image)
5. Select the "Visible" property in the "Bindable properties" list.
6. For "Custom binding" section, type: Bind("IsImportant").
7. It's done.
When the "IsImportant" value is true, the image will be shown.
Otherwise, the image will be invisible.

If you know there is any simpler or better way to do this, please let me know. ^_^

No comments:

Post a Comment