1,105,585 Community Members

ListBox C#

Member Avatar
vietnamese
Newbie Poster
8 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all!

In my Windows Forms Application (C# 2005), I am using ListBox but it have a problem when I remove item.
listBox.Items.RemoveAt(1); ---> Items collection cannot be modified when the DataSource property is set.

Note: It have just appeared when I used listbox.DataSource = dataTable;
If I dont use DataSource --> Everything is OK

For(int i=0; i<10; i++)
 listbox.item.Add(i.ToString());

listBox.Items.RemoveAt(1); ---> OK

Thanks in Adv

Member Avatar
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

The anwer is in your post. Simply don't use binding.

Regards,
Camilo

Member Avatar
vietnamese
Newbie Poster
8 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

The anwer is in your post. Simply don't use binding.

Regards,
Camilo

Microsoft supports thi datasource for us, why dont we use it? Could you explain to me?
If I dont use binding, How can I add values to this one like a ComboBox (DisplayMember, ValueMember)
Thanks adv

Member Avatar
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

"Microsoft supports thi datasource for us, why dont we use it? Could you explain to me?"

I think that the logic is like fallows. When you bind a DataTable to a ListBox what you get is a reference to the DataTable if you change any value in the ListBox you would be actually afecting any other object that is also bindded to that DataTable. So in my opinion that might be the motive. Also in OOP I don't think that is 'good practice' to change tha value of an object indirectly. (e.i Changing the value of the ListBox you are inded changing the value of the DataSet).

Hope this help,
Regards,
Camilo

Member Avatar
vietnamese
Newbie Poster
8 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks Camilo,
But how can we add value into ListBox likes(DisplayMember, ValueMember)?

Member Avatar
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

I don't know if this is the best way to do it but my idea is to create a class for mapping the results that you get from the database and since the ListTextBox.Items.Add takes an object as parameter then you can pass an instance of that class that you had created.

Hope this help.
Camilo

Member Avatar
vietnamese
Newbie Poster
8 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ah, that is an idea anyway. Thank you :)

Member Avatar
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

A littler late but this is all you need to do.

dataTable.Rows.RemoveAt(this.listBox.SelectedIndex);

of course like a said in one of the above posts this will update any other Control that is bound to that DataTable if any.

Regards,
Camilo

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article