Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 655633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:37:34+00:00 2026-05-13T22:37:34+00:00

I want to populate GridView below with images: <asp:GridView ID=GrdDynamic runat=server AutoGenerateColumns=False> <Columns> </Columns>

  • 0

I want to populate GridView below with images:

<asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False">
    <Columns>
    </Columns>
</asp:GridView>

The code below iterates through directory, then I collect image titles and want them to be populated in gridview. code in bold is not working well, gridview is only filled with the last image in list.

List<string> imagelist = new List<string>();

protected void Page_Load(object sender, EventArgs e)
{
    foreach (String image in Directory.GetFiles(Server.MapPath("example/")))
    {
        imagelist.Add("~/example/" + Path.GetFileName(image));
    }
    loadDynamicGrid(imagelist);
}

private void loadDynamicGrid(List<string> list)
{
    DataTable dt = new DataTable();

    DataColumn dcol = new DataColumn(NAME, typeof(System.String));
    dt.Columns.Add(dcol);

    dcol = new DataColumn("NAME1", typeof(System.String));
    dt.Columns.Add(dcol);

    dcol = new DataColumn("NAME2", typeof(System.String));
    dt.Columns.Add(dcol);

    dcol = new DataColumn("NAME3", typeof(System.String));
    dt.Columns.Add(dcol);

    DataRow drow = dt.NewRow();
            dt.Rows.Add();
            dt.Rows.Add();

            **for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    foreach (string value in list)
                    {
                        dt.Rows[i][j] = value;
                    }
                }
            }**

    foreach (DataColumn col in dt.Columns)
    {
        ImageField bfield = new ImageField();

        bfield.DataImageUrlField = NAME;

        bfield.HeaderText = col.ColumnName;

        GrdDynamic.Columns.Add(bfield);
    }

    GrdDynamic.DataSource = dt;

    GrdDynamic.DataBind();
}

how to fill gridview cell-by-cell only with available amount of images?

i know it is easy, i tried various methods like: dt.Rows.Add(list); and some other attempts, but they didn’t work. i’m very stupid.
i’d be glad for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:37:34+00:00Added an answer on May 13, 2026 at 10:37 pm

    You’re repeatedly setting every cell to every string with your foreach. You need to addess a specific element in list that will correspond to the specific cell. Try something like this:

    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
        for (int j = 0; j < dt.Columns.Count; j++) 
        { 
            //below line assumes list has more than i*j elements
            //how you want to handle anything outside that case 
            //is not clear
            dt.Rows[i][j] = list[(i * dt.Columns.Count) + j] ;                     
        } 
    }
    

    Edit: Reading your question more carefully, your problem is fundamentally outside of that loop. Even if you applied the above fix, you still wouldn’t get what you’re hoping for. What you need to do is loop over the list, and add each item in the list a row, and add that row to the table. Something like this:

    int currColIndex = 0;
    DataRow drow = dt.NewRow();
    foreach(string value in list)
    {
        if(currColIndex >= dt.Columns.Count) 
        { 
            dt.Rows.Add(drow);
            drow = dt.NewRow(); 
            currColIndex = 0;
        }
        drow[currColIndex++] = value;    
    }
    if(currColIndex != dt.Columns.Count)
    {
        dt.Rows.Add(drow);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<asp:GridView ID=gridInboxMessage runat=server AutoGenerateColumns=False DataSourceID=LinqDataSource1> <Columns> <asp:BoundField DataField=Title HeaderText=title ReadOnly=True SortExpression=Title /> <asp:BoundField DataField=Body
For example, I want to populate a gridview control in an ASP.NET web page
I want to hide ID column in my GridView, I knew the code GridView1.Columns[0].Visible
Consider the current algorithm below that iterates through a GridView 's rows to find
I want to populate a table, defined in layout xml file through the programmatic
I've been using some code very similar to the following to populate a gridview
I want to create a gridview like android market, I want to populate this
I'm using a form to populate a gridview with x users. However I want
I want to populate a gridview with table data from SQL. I tried just
I have a project with c# and Microsoft SQL Server, I want to populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.