C
C#2y 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
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
XogggOP2y 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
Yah that sounds right
Xoggg
XogggOP2y 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
NB: whatever you’re selecting out; so could be that you don’t have all columns
Xoggg
XogggOP2y ago
Makes since.
Mayor McCheese
So did that work?
Xoggg
XogggOP2y 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
It’s a start Probably good enough for the moment 🙂
Xoggg
XogggOP2y ago
For my school assignment, most definitely. I dont ever see myself needing to utilize DataSources in the future honestly.
Mayor McCheese
Why not? I mean ideally you’re doing some sort of data binding
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server