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 MunshiPosted Feb 13, 2012, 4:09 AM
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.
Piyush ChandralaPosted Oct 18, 2014, 2:22 PM
Satyapriya NayakPosted Feb 13, 2012, 4:32 AM
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
shubhangi khandalePosted Feb 13, 2012, 4:14 AM
we are use windows application and GetSelectedIndices() this method in web
Suthish NairPosted Feb 13, 2012, 3:54 AM
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 + " ";
}
}
shubhangi khandalePosted Feb 13, 2012, 3:51 AM
Abdullah MunshiPosted Feb 13, 2012, 3:35 AM
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 khandalePosted Feb 13, 2012, 2:54 AM
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
shubhangi khandalePosted Feb 13, 2012, 2:41 AM
shubhangi khandalePosted Feb 13, 2012, 2:39 AM
Satyapriya NayakPosted Feb 13, 2012, 2:29 AM
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
Abdullah MunshiPosted Feb 13, 2012, 2:15 AM
string s = "";
foreach (ListItem li in ListBox1.Items) {
if (li.Selected == true) {
s = s +" "+ li.Value;
}
}