0

我有一个ListBox, on_button 单击,我希望 中的所有项目都ListBox显示为GridView. 这是我尝试过的代码:

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"));
        }
    }

我希望 中的所有项目都ListBox显示为 中的标题字段GridView

上面的代码没有给出任何错误,但是运行时它不起作用。任何人都可以帮我解决这个问题吗?

这是我的设计代码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>
4

1 回答 1

2

设置好DataSource后,调用DataBind方法看看会发生什么

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

编辑

下面的代码工作得很好。经测试。

如果网格中没有数据,它甚至不会显示任何内容,除非您为网格提供 EmptyText 字段属性值。

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();

此链接中提供了工作示例。

于 2012-08-24T17:27:32.627 回答