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 8426427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:31:53+00:00 2026-06-10T04:31:53+00:00

I have a ListBox , on_button click, I want all the items in the

  • 0

I have a ListBox, on_button click, I want all the items in the ListBox to be displayed as headers in the GridView.
Here is the code that i have tried :

protected void DONE4_Click(object sender, EventArgs e)
    {
        Panel7.Visible = true;

        DataTable dt = new DataTable();
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            dt.Columns.Add(ListBox1.Items[i].ToString());
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();

        foreach (GridViewRow grdRow in GridView2.Rows)
        {
            DropDownList bind_dropdownlist = new DropDownList();                                                    // defining the property of the DropDownList as bind_dropdownlist
            bind_dropdownlist = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[0].FindControl("Pro_List"));   // finding the  DropDownList from the gridiew for binding
            SqlDataAdapter mydata = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
            DataSet dset = new DataSet();                                                                           // binding the DropDownList with the dataset ds
            mydata.Fill(dset, "Table");
            bind_dropdownlist.DataSource = dset;
            bind_dropdownlist.DataTextField = "Profile_Instance";                                                   // set the DropDownList's DataTextField as designation which display the designation in the dropdownlist after fetching the data from database
            bind_dropdownlist.DataBind();
            bind_dropdownlist.Items.Insert(0, new ListItem("---Choose Profile---", "-1"));
        }
    }

I want all the items in the ListBox to be displayed as header field in a GridView.

The code above gives no errors, but when run it does not work. Can anyone kindly help me with this?

Here is my Design code for the GridView:

<asp:Panel ID="Panel7" runat="server">
        <asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" style="text-align: center; font-size: small">

            <Columns>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:DropDownList ID="Pro_List" runat="server">
                        <asp:ListItem>--Select--</asp:ListItem>                          
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </asp:Panel>
  • 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-06-10T04:31:54+00:00Added an answer on June 10, 2026 at 4:31 am

    After setting the DataSource, call the DataBind method and see what happens

    GridView2.DataSource = dt;
    GridView2.DataBind();
    

    Edit

    The below code works just fine. Tested.

    If you do not have data in the Grid, it won’t show anything even the headers unless you give an EmptyText field property value for the grid.

    DataTable dt = new DataTable();
    DataRow rw = default(DataRow);
    for (int i = 0; i < ListBox1.Items.Count; i++)
    {
        dt.Columns.Add(ListBox1.Items[i].ToString(),
                                       System.Type.GetType("System.String"));             
    }
    //Simply adding 10 rows
    //Replace this hard coded loop with your looping 
    // over your data to add rows.
    for (int j = 0; j < 10; j++)
    {
        rw = dt.NewRow();
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            rw[ListBox1.Items[i].ToString()] = "Hello there";
        }              
        dt.Rows.Add(rw);
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
    

    The working sample is available in this link.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that deselect all selected items in the listbox when the
I have some code that will load full file names (ex.F:\logs\1234.log) into a listbox
I have multiselect list box, I want to clear all selected items On clear
I have a ListBox that contains duplicate items. From what I gather, ListBox.SelectedItems will
I have a databound listbox on my C# WinForm that holds strings that are
I have Window that has a ListBox ListBox(MyListBox) has a DataTable for its DataContext
I want to display all the project 's tasks in a page, given that
I have a UserControl that incorporates a textbox. I want to set the keyboardfocus
I have a listbox with runat=server Items are added to this listbox on the
I want to dispaly a list on a button click.I have added a list

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.