Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i m having listbox item as
<br />
test1<br />
test2<br />
test3<br />
test4<br />
test5<br />


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
Posted
Updated 9-May-11 1:19am
v4
Comments
Sandeep Mewara 9-May-11 1:21am    
Tried anything? Can you be more specific? Start from MSDN.. then Google..
beginner in C#.net 9-May-11 1:42am    
i m having one textbox,listbox i added list item from textbox using listbox.items.add.
then i want to show these listbox item in gridview without using database.give me some solution sir

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

}
 
Share this answer
 
v2
Comments
beginner in C#.net 9-May-11 1:37am    
i want to use listbox item in gridview without using database...
LakshmiNarayana Nalluri 9-May-11 3:44am    
generally i told database without database also we can. as i said previously first you create dynamic table and create columns and then add rows with textbox values ,give it to gridview datasource. i had done it. try it .if u don't know i will send my code.try first.you can do.
beginner in C#.net 9-May-11 4:09am    
sir i used dis code
protected void Btn1_Click1(object sender, EventArgs e)
{
List1.Items.Add(Txt1.Text);
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]] = "asdf";(instead of dis string how i use listbox value in this column)

dt.Rows.Add(dr);

Grd1.DataSource = dt;
Grd1.DataBind();
}
in item template <%#eval("Dc")%>

when i use string it shows..
but i want data from listbox
LakshmiNarayana Nalluri 11-May-11 2:07am    
Don't forget mark as answer
try out in the same way

XML
<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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900