Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make items in the ListBox as headers of GridView
    primarykey
    data
    text
    <p>I have a <code>ListBox</code>, on_button click, I want all the items in the <code>ListBox</code> to be displayed as headers in the <code>GridView</code>. Here is the code that i have tried :</p> <pre><code>protected void DONE4_Click(object sender, EventArgs e) { Panel7.Visible = true; DataTable dt = new DataTable(); for (int i = 0; i &lt; 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")); } } </code></pre> <p>I want all the items in the <code>ListBox</code> to be displayed as header field in a <code>GridView</code>.</p> <p>The code above gives no errors, but when run it does not work. Can anyone kindly help me with this?</p> <p>Here is my Design code for the <code>GridView</code>:</p> <pre><code>&lt;asp:Panel ID="Panel7" runat="server"&gt; &lt;asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" style="text-align: center; font-size: small"&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=""&gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList ID="Pro_List" runat="server"&gt; &lt;asp:ListItem&gt;--Select--&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/asp:Panel&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload