Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used this code to fill my table which is contain image and the name of it ,
so if i have 6 items , i don't want them to display in two rows ( one for image and second for name )
i need to display only three values in each row
which mean :
first row ( image1 , image2 , image3 )
second row (name1 , name2 , name3)
third row (image4 , image5 ,image6 )
fourth row ( name4 , name5 , name6 )
C#
void fillTable()
{
    ProductManagement pm = new ProductManagement();
    DataTable dt = pm.Product_Random();
    StringBuilder html = new StringBuilder();

    //Table start.
    html.Append("<table border = '1'>");

    html.Append("<tr>");
    foreach (DataRow row in dt.Rows)
    {
        int windex = row["Image"].ToString().IndexOf("/");
        html.Append("<td><img src='" + row["Image"].ToString().Substring(windex) + "' height='60' width='60' </></td>");
    }

    html.Append("</tr>");

    html.Append("<tr>");
    foreach (DataRow row in dt.Rows)
    {
        int windex = row["Image"].ToString().IndexOf("/");
        html.Append("<td>" + row["Name"] + "</td>");
    }
    html.Append("</tr>");

    html.Append("</table>");

    //Append the HTML string to Placeholder.
    PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
}
Posted
Updated 19-Dec-14 8:39am
v3

1 solution

I am not entirely certain what you question is, but I believe you were wanting to know how to accomplish the layout you specified that looks like:

first row ( image1 , image2 , image3 )
second row (name1 , name2 , name3)
third row (image4 , image5 ,image6 )
fourth row ( name4 , name5 , name6 )

To do that use the following code.

C#
html.Append("<table border="1">");

int itemCount = 0;
StringBuilder sbImageRow = new StringBuilder();
StringBuilder sbNameRow = new StringBuilder();
foreach (DataRow row in dt.Rows)
{
    itemCount++;
    int windex = row["Image"].ToString().IndexOf("/");
    sbImageRow.Append("<td><img src='" + row["Image"].ToString().Substring(windex) + "' height='60' width='60' </></td>");
    sbNameRow.Append("<td>" + row["Name"] + "</td>");

    if(itemCount % 3 == 0)
    {
        //Add Image Row
        html.Append("<tr>");
        html.Append(sbImageRow.ToString());
        html.Append("</tr>");

        //Add Name Row
        html.Append("<tr>");
        html.Append(sbNameRow.ToString());
        html.Append("</tr>");

        //Reset values for next group
        itemCount = 0;
        sbImageRow = new StringBuilder();
        sbNameRow = new StringBuilder();
    }
}

//Check if some of the data hasn't been added to table yet
if(itemCount != 0)
{
    //Pad with empty cells if needed
    for(int i = itemCount; i < 3; i++)
    {
        sbImageRow.Append("<td></td>");
        sbNameRow.Append("<td></td>");
    }

    //Add Image Row
    html.Append("<tr>");
    html.Append(sbImageRow.ToString());
    html.Append("</tr>");

    //Add Name Row
    html.Append("<tr>");
    html.Append(sbNameRow.ToString());
    html.Append("</tr>");
}

html.Append("</table>");


Again, not sure if this what you were looking for and this isn't the most eloquent solution, but it would accomplish what you want. Please let me know if you have any questions.
 
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