在GridView中显示列表框项 [英] showing listbox item in gridview

查看:94
本文介绍了在GridView中显示列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将列表框项设置为

<br />
test1<br />
test2<br />
test3<br />
test4<br />
test5<br />



我从文本框中获取此值...我想在gridview中获取所有值.我尝试



i get this value from textbox... i want to get all value in gridview.. i tried

protected void List1_Load(object sender, EventArgs e)
   {
       DataSet ds = new DataSet();
       DataTable dt = new DataTable("Dt");
       DataColumn dc = new DataColumn("Dc");
       dc.DataType = typeof(string);
       dt.Columns.Add(dc);
       DataRow dr = dt.NewRow();
       dr[dt.Columns[0]] = List1.Text;
       dt.Rows.Add(dr);
       Grd1.DataSource = dt;
       Grd1.DataBind();
   }


它只显示选定的值...
我想获得所有价值
请给我一些解决方案


it only shows selected value...
i want to get all value
please give me some solution

推荐答案

像gridview一样,列表框也用于显示表数据,但只显示一列.确定,根据您的要求创建动态表并为其输入列表框项目值,然后将该表用作gridview的数据源.



受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!Page.IsPostBack)
{
DataTable dtToGrid = new DataTable();
dtToGrid.Columns.Add("UserName",typeof(string));
dtToGrid.Columns.Add("Password",typeof(string));
Session ["dtToGrid"] = dtToGrid;
}
}
受保护的void Button1_Click(对象发送者,EventArgs e)
{
DataTable dtToGrid =(DataTable)Session ["dtToGrid"];
DataRow drToGrid = dtToGrid.NewRow();
drToGrid ["UserName"] = TextBox1.Text.Trim();
drToGrid ["Password"] = TextBox2.Text.Trim();
dtToGrid.Rows.Add(drToGrid);
GridView1.DataSource = dtToGrid;
GridView1.DataBind();
TextBox1.Text =";
}
受保护的void Button2_Click(对象发送者,EventArgs e)
{
//保存在Sql Server表中
cn.Open();
DataTable dt =(DataTable)Session ["dtToGrid"];
使用(SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.DestinationTableName ="userdetails";
copy.WriteToServer(dt);
}
Page.RegisterStartupScript(& lt; script& gt;",& lt; script& alert("成功保存)& lt//script& gt;"));

}
like gridview, listbox also use to show table data but single column. ok as per your requirement create dynamic table and give listbox item values it''s input,and then use that table as datasource to gridview.



protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtToGrid = new DataTable();
dtToGrid.Columns.Add("UserName", typeof(string));
dtToGrid.Columns.Add("Password", typeof(string));
Session["dtToGrid"] = dtToGrid;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dtToGrid = (DataTable)Session["dtToGrid"];
DataRow drToGrid = dtToGrid.NewRow();
drToGrid["UserName"] = TextBox1.Text.Trim();
drToGrid["Password"] = TextBox2.Text.Trim();
dtToGrid.Rows.Add(drToGrid);
GridView1.DataSource = dtToGrid;
GridView1.DataBind();
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
//save in Sql Server Table
cn.Open();
DataTable dt = (DataTable)Session["dtToGrid"];
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.DestinationTableName = "userdetails";
copy.WriteToServer(dt);
}
Page.RegisterStartupScript("&lt;script&gt;", "&lt;script&gt;alert(''Successfully Saved'')&lt;/script&gt;");

}


以相同的方式尝试

try out in the same way

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"

            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Visible="False">
            <Columns>
                 <asp:TemplateField HeaderText="First">
                    <ItemTemplate>
                        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" HeaderText="Edit" />
            </Columns>
        </asp:GridView>


这篇关于在GridView中显示列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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