Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I did this to display multiple images row wise, but the image keeps displaying on the same row.
The page should show images in this format:
----------------------------------------
|                 |                   |
|                 |                   |
|   Image 1       |     Image 2       |
|                 |                   |
|                 |                   |
----------------------------------------
----------------------------------------
|                 |                   |
|                 |                   |
|   Image 3       |     Image 4       |
|                 |                   |
|                 |                   |
----------------------------------------


I did this
but lacking above view.
TableRow tRow = new TableRow();
     for (int i = 0; i < sqlDt.Rows.Count; i++)
     {
         TableCell tCell = new TableCell();
         Image myImg = new Image
         {
             ImageUrl = sqlDt.Rows[i]["MyImagePath"].ToString(),
             AlternateText = sqlDt.Rows[i]["Name"].ToString(),
             Width = 100,
             Height = 100
         };
         tCell.Controls.Add(myImg);
         Label l = new Label
         {
             Text = "<br>" + sqlDt.Rows[i]["Name"].ToString(),
         };
         l.Style.Add("font-weight", "bold");
         l.Style.Add("text-align", "center");
         tCell.Controls.Add(l);

         tRow.Cells.Add(tCell);
     }
     myTable.Rows.Add(tRow);


How to achieve this?
Posted

Try this.. I hope this is helpfull to you
int count=0;
for (int i = 0; i < sqlDt.Rows.Count; i++)
{
TableRow tRow = new TableRow();
TableCell tCell = new TableCell();
Image myImg = new Image
{
ImageUrl = sqlDt.Rows[i]["MyImagePath"].ToString(),
AlternateText = sqlDt.Rows[i]["Name"].ToString(),
Width = 100,
Height = 100
};
tCell.Controls.Add(myImg);
Label l = new Label
{
Text = "" + sqlDt.Rows[i]["Name"].ToString(),
};
l.Style.Add("font-weight", "bold");
l.Style.Add("text-align", "center");
tCell.Controls.Add(l);
tRow.Cells.Add(tCell);
count++;

if(count%2==0)
{
myTable.Rows.Add(tRow);
}
if(i==sqlDt.Rows.Count-1 && count%2!=0)
{
myTable.Rows.Add(tRow);
}


}
 
Share this answer
 
Comments
Member 10450902 11-Jul-14 13:02pm    
what is sqlDt here can you please explain?
Anaya Upadhyay 18-Aug-14 1:24am    
a datatable
C#
DataTable dt = GetDataFromDatabase();

var sb = new System.Text.StringBuilder();
sb.AppendLine();
sb.AppendLine("<table>");

int cellCount = 0;

foreach (DataRow dr in dt.Rows)
{
    cellCount = cellCount + 1;

    if (cellCount == 1)
        sb.AppendLine("<tr>");

    string imagepath = dr["MyImagePath"] + "";
    string name = dr["Name"] + "";

    sb.AppendLine("<td style=\"font-weight: bold; text-align: center;\">");
    sb.Append("<img src=\"");
    sb.Append(imagepath);
    sb.Append("\" style=\"height: 100px; width: 100px;\" alt=\"");
    sb.Append(name);
    sb.Append("\" /><br />");
    sb.AppendLine(name);
    sb.AppendLine("</td>");

    if (cellCount >= 2)
    {
        sb.AppendLine("</tr>");
        cellCount = 0;
    }
}

if (cellCount == 1)
    sb.AppendLine("<td></td></tr>");

sb.AppendLine("</table>");

Form.Controls.Add(new LiteralControl(sb.ToString()));
 
Share this answer
 
v3

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