简体   繁体   中英

How do I make items in the ListBox as headers of GridView

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>

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 wont 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM