How to display multiple selected values from listbox to gridview and store them in database?
For eg. If I select 3 values from listbox all the 3 values should be seen in Gridview. But I am getting only the first selected value in Gridview. Please help with this problem. Thanks in advance.
Sanjeeb LenkaPosted Nov 26, 2013, 4:32 AM
in aspx:
in cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("item", typeof(int));
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
{
DataRow dr = table.NewRow();
dr["item"] = li.Value;
table.Rows.Add(dr);
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
}
DhanashriPosted Nov 26, 2013, 5:03 AM