Hi
How to get listbox multiple selected values in label or string.
I have bind data from table in listbox then when i select multi values then this display in label or string
when i try get values in label then display System.Data.Dataview multi time
my code
for (i = 0; i < lstcsuspectcode.SelectedItems.Count; i++)
{
label18.Text = label18.Text + " " + lstcsuspectcode.SelectedValue;
}
Loading

Abdullah Munshi51 reputationPosted Feb 12, 2012, 11:09 PM EST
string s = "";
foreach (DataRowView row in listBox1.SelectedItems) {
s = s + " " + row.Row["name"].ToString();
}
label1.Text = s;
================
Replace "name" with your column name that you display in listbox.
Suthish NairRank #92 · 22.8K reputationPosted Feb 12, 2012, 10:54 PM EST
another sample...
onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="85px">
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (int item in ListBox1.GetSelectedIndices())
{
lbl.Text += ListBox1.Items[item].Text + " ";
}
}
Abdullah Munshi51 reputationPosted Feb 12, 2012, 9:15 PM EST
string s = "";
foreach (ListItem li in ListBox1.Items) {
if (li.Selected == true) {
s = s +" "+ li.Value;
}
}
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 12, 2012, 9:29 PM EST
Try this...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("a");
listBox1.Items.Add("b");
listBox1.Items.Add("c");
listBox1.Items.Add("d");
}
private void button1_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < listBox1.SelectedItems.Count; i++)
{
label1.Text = label1.Text + " " + listBox1.SelectedItem.ToString();
}
}
}
}
Thanks
shubhangi khandale55 reputationPosted Feb 12, 2012, 9:39 PM EST
shubhangi khandale55 reputationPosted Feb 12, 2012, 9:41 PM EST
shubhangi khandale55 reputationPosted Feb 12, 2012, 9:54 PM EST
can u give me other solution for my post.
i have design form in this form course listbox which bind form coursetable.
when student fill this form he can select multi course from listbox then display in label which selected courses
so any other solution for this......... plz
Abdullah Munshi51 reputationPosted Feb 12, 2012, 10:35 PM EST
string s = "";
foreach (object item in listBox1.SelectedItems) {
s = s+" "+ item.ToString();
}
label1.Text = s;
======
That previous code was for webapplication. So it gave error missing assembly reference.
Try this code.
shubhangi khandale55 reputationPosted Feb 12, 2012, 10:51 PM EST
shubhangi khandale55 reputationPosted Feb 12, 2012, 11:14 PM EST
we are use windows application and GetSelectedIndices() this method in web
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 12, 2012, 11:32 PM EST
Try this...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Listbox2
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from emp";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "emp");
dt = ds.Tables["emp"];
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
listBox1.Items.Add(dt.Rows[i].ItemArray[0]);
}
con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < listBox1.SelectedItems.Count; i++)
{
label1.Text = label1.Text + " " + listBox1.SelectedItem.ToString();
}
}
}
}
Thanks
Piyush Chandrala71 reputationPosted Oct 18, 2014, 10:22 AM EDT