views:

281

answers:

2

i have a database of country table. sql 2005 like,

country_id  country_name   region       area    population   gdp        status
   84        indi         asia      3432     3423           343         Active
   85        U.k          Europe   43432     3243          2343         Inactive

I want to user image of active and inactive instead of text in my datagrid .. not in database.. in database there is text only..

so.. i am using .net 2005 .. i want to bind image with status.. and whenever i changed active to inactive or inactive to active.. image will be change . according that..

i am using C# as code behind language..

+1  A: 

Simplest way is to add an image column like that :

<asp:ImageField 
    DataImageUrlField="status" 
    DataImageUrlFormatString="~/images/{0}.jpg">
</asp:ImageField>

And you should have two images like active.jpg and inactive.jpg in your images folder.

By the way if you have null values in your status column don't forget to set NullImageUrl property for them.

EDIT : Actually I see you're looking for DataGrid, I missed it, here is the datagrid way with template column :

<asp:TemplateColumn>
    <img src='<%# DataBinder.Eval(Container.DataItem, "status", "~/images/{0}.jpg") %>' />
</asp:TemplateColumn>

And this one with the asp:image server control :

<asp:TemplateColumn>
    <asp:Image runat="server" ID="imgStatus" 
        ImageUrl='<%# DataBinder.Eval(Container.DataItem, "status", "~/images/{0}.jpg") %>' />
</asp:TemplateColumn>
Canavar
I can't get your question ? But sorting will be no problem. if you set the right location of the images, it should work well.
Canavar
is it used in datagrid.. because.. i want to use it. in datagrid..
Sikender
<asp:BoundColumn HeaderText=" Status" DataField="status" SortExpression="status" ItemStyle-Width="140" ></asp:BoundColumn>this is my column of status.. where i put you code.. into templet or where?
Sikender
No, just replace your column with mine. Add the properties to mine column too : HeaderText=" Status" SortExpression="status" ItemStyle-Width="140"
Canavar
imagefield.. is not suitable .to used in datagrid.. i tried it..
Sikender
imagefield is not a valid tools for datagrid...
Sikender
can u set this line with your img src line.. because.. img src is not suitable.. for datagrid.. its given error..<asp:image />what will be used inside.. can u tell me.. pls
Sikender
thx.. buddy thank you very much...man...
Sikender
+1  A: 

ImageField was introduced in ASP.Net 2.0 for GridView. Here's a good article explaining it (with screenshots).

To display an image in a column in a DataGrid in ASP.NET 1.x you have to use a TemplateColumn with an Image Web control inside the TemplateColumn.

DOK
but i am using... datagrid.. thats why?
Sikender