Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to pass all listbox items (numbers 1 and 2 in this case) to textbox and I have this code

C#
for (int i = 0; i < listpatternID.Items.Count; i++)
{
if ((i + 1) < listpatternID.Items.Count)
patternID1.Text += listpatternID.Items[i] + ", ";
else
patternID1.Text += listpatternID.Items[i];
}


but in textbox I see System.Data.DataRowView, System.Data.DataRowView

I dont see 1 and 2

where am I wrong????I know I am wrong somewhere :)
Posted
Updated 27-Aug-12 1:22am
v2

This is because you are accessing object while string concatenation.
Here: patternID1.Text += listpatternID.Items[i];

Change it to: patternID1.Text += listpatternID.Items[i].Text;
Refer: MSDN: ListControl.Items Property [^]
 
Share this answer
 
Comments
__TR__ 27-Aug-12 8:23am    
5+
Manas Bhardwaj 27-Aug-12 8:45am    
5+
You need to use Text property to get text.

C#
for (int i = 0; i < listpatternID.Items.Count; i++)
{
    if ((i + 1) < listpatternID.Items.Count)
        patternID1.Text += listpatternID.Items[i].Text + ", ";
    else
        patternID1.Text += listpatternID.Items[i].Text;
}
 
Share this answer
 
v2
Comments
shonezi 27-Aug-12 7:32am    
it says

object does not contain a definition for text and no extension method text accepting first argument of type object could be found (are you missing a using directive or assembly reference?)


I have these using directives

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.Configuration;
using User.Properties;
using System.Data.SqlClient;
Prasad_Kulkarni 27-Aug-12 8:17am    
My 5
Manas Bhardwaj 27-Aug-12 8:34am    
thx
__TR__ 27-Aug-12 8:22am    
5+
Manas Bhardwaj 27-Aug-12 8:34am    
thx
Hi ,
Check this
C#
foreach (DataRowView item in listpatternID.Items)
        {
            textBox1.Text += item[0].ToString()  + "  , " ;
        }

Best Regards
M.Mitwalli
 
Share this answer
 
v4
Comments
shonezi 27-Aug-12 7:35am    
I did this but nothing, again the same
Mohamed Mitwalli 27-Aug-12 7:43am    
how you are fill your listbox
shonezi 27-Aug-12 7:46am    
daPattern.SelectCommand = new SqlCommand("SELECT * from Pattern", cs);
dsPattern.Clear();
daPattern.Fill(dsPattern);

PatternBS.DataSource = dsPattern.Tables[0];


listpatternID.DataSource = PatternBS;
listpatternID.DisplayMember = "ID";
Mohamed Mitwalli 27-Aug-12 8:11am    
Check the Update now
shonezi 27-Aug-12 8:13am    
THANK YOU!!!!!!!
Maybe it helps:
C#
for (int i = 0; i ; this.listBox1.Items.Count - 1; i++)
{
  this.textBox1.Text += string.Format("{0}, ",
          ((this.listBox1.Items[i] as DataRowView).Row.ItemArray[0].ToString()));
}
 
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