C
C#16mo ago
Xoggg

❔ Copying selected item from list box to another list box (Utilizing data source)

I cant seem to be able to copy one item from a listbox to another, my current code I am attempting is as so listbox3.items.add(listbox2.selecteditem.tostring()); It is adding an item to my list box, but it reads System.Data.DataRowview instead of the selected item text, I think this is due to me populating my initial listbox from a data source. Is there an easy work around for this? I couldnt seem to find anything like this issue online.
12 Replies
Mayor McCheese
Mayor McCheese16mo ago
This is winforms I suppose; regardless DataRowView has columns you can access data with; probably easiest to cast SelectedItem to DataRowView and access the column you need to get the needed data. https://learn.microsoft.com/en-us/dotnet/api/system.data.datarowview?view=net-8.0
Xoggg
Xoggg16mo ago
Correct on the winforums portion; If I were to cast to DataRowView, would I then have access to the properties of that row (In this case the colums of my MDF database table)?
Mayor McCheese
Mayor McCheese16mo ago
Yah that sounds right
Xoggg
Xoggg16mo ago
Ah I see let me give this a shot. I never think about casting in C#, I usually only work with simple data types.
Mayor McCheese
Mayor McCheese16mo ago
NB: whatever you’re selecting out; so could be that you don’t have all columns
Xoggg
Xoggg16mo ago
Makes since.
Mayor McCheese
Mayor McCheese16mo ago
So did that work?
Xoggg
Xoggg16mo ago
foreach (DataRowView selectedItem in listBox3.SelectedItems)
{
DataRowView dr = (DataRowView)selectedItem;
String result = dr["itemName"].ToString();
listBox4.Items.Add(result);
}
foreach (DataRowView selectedItem in listBox3.SelectedItems)
{
DataRowView dr = (DataRowView)selectedItem;
String result = dr["itemName"].ToString();
listBox4.Items.Add(result);
}
Yes, it did thank you. This was how I implemented the solution.
Mayor McCheese
Mayor McCheese16mo ago
It’s a start Probably good enough for the moment 🙂
Xoggg
Xoggg16mo ago
For my school assignment, most definitely. I dont ever see myself needing to utilize DataSources in the future honestly.
Mayor McCheese
Mayor McCheese16mo ago
Why not? I mean ideally you’re doing some sort of data binding
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.