如何让ListBox中的项目,如GridView控件的标题 [英] How do I make items in the ListBox as headers of GridView

查看:126
本文介绍了如何让ListBox中的项目,如GridView控件的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListBox ,on_button点击,我要在 ListBox中的所有项目显示为标题在 GridView控件
下面是我曾尝试code:

 保护无效DONE4_Click(对象发件人,EventArgs的发送)
    {
        Panel7.Visible = TRUE;        DataTable的DT =新的DataTable();
        的for(int i = 0; I< ListBox1.Items.Count;我++)
        {
            dt.Columns.Add(ListBox1.Items [I]的ToString());
        }
        GridView2.DataSource = DT;
        GridView2.DataBind();        的foreach(GridViewRow grdRow在GridView2.Rows)
        {
            DropDownList的bind_dropdownlist =新的DropDownList(); //定义为bind_dropdownlist DropDownList的财产
            bind_dropdownlist =(DropDownList中)(GridView2.Rows [grdRow.RowIndex] .Cells [0] .FindControl(Pro_List)); //找到的DropDownList从gridiew绑定
            SqlDataAdapter的MYDATA =新的SqlDataAdapter(SELECT DISTINCT Profile_Instance FROM Profile_MasterCON);
            数据集DSET =新的DataSet(); //绑定与数据集DS将DropDownList
            mydata.Fill(DSET,表);
            bind_dropdownlist.DataSource = DSET;
            bind_dropdownlist.DataTextField =Profile_Instance; //设置DropDownList的DataTextField指定为获取其从数据库中的数据显示后,在DropDownList指定
            bind_dropdownlist.DataBind();
            bind_dropdownlist.Items.Insert(0,新的ListItem(---选择配置文件---, - 1));
        }
    }

我想在的ListBox 所有项目中显示为报头字段一个 GridView控件

上面的code给出任何错误,但是在运行时它不工作。谁能好心帮我?

下面是我设计的code为 GridView控件

 < ASP:面板ID =Panel7=服务器>
        < ASP:GridView控件ID =GridView2=服务器的cellpadding =4前景色=#333333
            网格线=无的风格=文本对齐:中心;字体大小:小>            <柱体和GT;
            < ASP:的TemplateField的HeaderText =>
                <&ItemTemplate中GT;
                    < ASP:DropDownList的ID =Pro_List=服务器>
                        < ASP:ListItem的> - 选择 - < / ASP:ListItem的>
                    < / ASP:DropDownList的>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
            < /专栏>
        < / ASP:GridView的>
    < / ASP:面板>


解决方案

设定DataSource的后,调用的DataBind 方法,并看看会发生什么。

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

编辑:以下code的作品就好。测试。

如果你没有在网格数据,它不会显示任何东西甚至是头,除非你给网格的EmptyText字段属性值。

  DataTable的DT =新的DataTable();
DataRow的RW =默认(DataRow的);
的for(int i = 0; I< ListBox1.Items.Count;我++)
{
    dt.Columns.Add(ListBox1.Items [I]的ToString()
                                   System.Type.GetType(System.String));
}
//简单地增加10行
//替换该硬codeD环与循环
//您的数据添加行。
为(中间体J = 0; J&小于10; J ++)
{
    RW = dt.NewRow();
    的for(int i = 0; I< ListBox1.Items.Count;我++)
    {
        RW [ListBox1.Items [I]的ToString()] =你好;
    }
    dt.Rows.Add(RW);
}
GridView1.DataSource = DT;
GridView1.DataBind();

工作样本是在这个链接可用。

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.

这篇关于如何让ListBox中的项目,如GridView控件的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆