Click here to Skip to main content
15,884,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,

I want to display an image from webserver's folder based on row values.
The database is pretty easy, just two columns (string username, int state) we'll get the state based on a WPF application and there are two states: 0,1
It works, that is nice but I want to display the results via the company's web-application platform.

I started with a simple GridView and added an extra column for the image:

ASP.NET
        <asp:GridView ID="Grid1" PageSize="50" DataSourceID="LinqStateSource" AllowPaging="True" OnRowDataBound="GridView1_RowDataBound"ShowFooter = "True" 
 AllowSorting="True" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="username" HeaderText="Username" SortExpression="username" />
        <asp:BoundField DataField="state" HeaderText="State" SortExpression="State" />
        <asp:ImageField HeaderText="Picture" />
    </Columns>
</asp:GridView>

<!--- LINQ! ---!>
<asp:LinqDataSource ID="LinqStateSource" runat="server" ContextTypeName="...WebApp.data.LinqStateSourceDataContext" EntityTypeName="" TableName="tblname"/>


It works so far, but now I want to show a picture (green, red dots) instead of the "state" column.

In Code behind I started with that:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           foreach (GridViewRow row in Grid1.Rows)
           {
               for (int i = 0; i < Grid1.Columns.Count; i++)
               {
                   /*if(e.Row.Cells[1].Text== "1")
                       e.Row.Cells[2].Image = @"...\images\greendot.ico";
                        |_______ "mindcode"
                   else
                       e.Row.Cells[2].Image = @"...\images\reddot.ico";
                        |_______ "mindcode"
                      /*
               }
           }

       }


I have no clue how to manage it.

Thank you,
Vincent

What I have tried:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           foreach (GridViewRow row in Grid1.Rows)
           {
               for (int i = 0; i < Grid1.Columns.Count; i++)
               {
                   /*if(e.Row.Cells[1].Text== "1")
                       e.Row.Cells[2].Image = @"...\images\greendot.ico";
                        |_______ "mindcode"
                   else
                       e.Row.Cells[2].Image = @"...\images\reddot.ico";
                        |_______ "mindcode"
                      /*
               }
           }

       }
Posted
Updated 2-Sep-16 6:09am
v2

I find it easier to do one of the following options:

OPTION 1: bind in markup:
1. Prepare your datasource to include the url on query (maybe put some "case when " style logic in your DB query). e.g. SELECT url AS imageUrl FROM myTable...
2. Bind image source url to control on markup
e.g.
<img src="<%#Eval("imageUrl")" />

OPTION 2: Add image control on row data bind
C#
string imageUrlValue = @"...\images\{0}.ico";
            DataRow myRow = e.Row.DataItem as DataRow;
            Image img = new Image();
            img.ImageUrl = 
                string.Format
                (
                    imageUrlValue,
                    myRow["Mycolumn"] == "1" ? "greendot" : "reddot"
                );
            row.Cells[2].Controls.Add(img);
 
Share this answer
 
v2
Another option: bind the image in markup, using a code-behind function to generate the URL:
ASP.NET
<asp:Image runat="server"
    ImageUrl='<%# BuildStateImageUrl((int)Eval("State")) %>'
/>

C#
protected string BuildStateImageUrl(int state)
{
    switch (state)
    {
        case 1:
        {
            return "~/images/greendot.png";
        }
        default:
        {
            return "~/images/reddot.png";
        }
    }
}

NB: Icon files (.ico) are not guaranteed to work in all browsers. You should convert the files to one of the standard web formats instead (.png, .jpg or .gif).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900