15819

Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value

I am creating a Windows form application in Visual Studio 2010. I have an Access database which has a table 'ResearcherInfo'.

In my windows form, I have a listbox which gets populated with the contents of the column name 'Researcher Name' of this table.

However when I try to get the selected items of this text box, it only returns me a null value or sometimes System.Data.DataRowView depending on how I access it

I have tried

string name = projectmembers_lst.SelectedItem.ToString();

While debugging when I hover the mouse over the 'projectmembers_lst' word, it correctly shows me projectmembers_lst = {SelectedItem = "Jerome"}

but on hovering over the string 'name', it shows "name = "System.Data.DataRowView""

i even tried copying to an array

foreach (Object selecteditem in projectmembers_lst.SelectedItems) { object[] objCollection = new object[projectmembers_lst.SelectedItems.Count]; projectmembers_lst.SelectedItems.CopyTo(objCollection, 0); MessageBox.Show(objCollection[0] as string); }

Nothing gets me the selected items. I am not sure what is wrong here.

<strong>Populating the List Box:</strong>

1) I have the added the access table 'ResearcherInfo'. as a data source for the web form application.

2) In the properties of the Listbox, under the Datasource tab, I have linked this table and given the display member and value member property of the Listbox as the column name 'Researcher Name' of the table.

So basically my Listbox displays the Researcher Names from the Researcher info table. attached is a snap shot. <img src="https://i.stack.imgur.com/ISIwk.jpg" alt="enter image description here">

Answer1:

without seeing how you're populating it, it's tough to guess.

try ((objCollection[0] as DataViewRow)[0] as string

Recommend

  • Check is Eval(“VALUE”) is null
  • C# WPF - Get the selected items from a ListView
  • Copy items from ListBox to CheckedListBox
  • C# DatagridView search/filter
  • MDX Get UserName Where UserID
  • Winsock returns 10061 on connect only to localhost
  • JavaFX: Mouse clipboard does not work in Unix
  • Toggle setLineDash()
  • Activation Function choice for Neural network
  • Django Haystack Rebuild Index
  • Ajax calls do not work in IE unless you fiddle with security settings
  • jQuery ready not fired after rails link_to is clicked
  • Keep this build forever option - Jenkins
  • Yii2: Config params vs. const/define
  • Algorithm for a smudge tool?
  • Regex thinks I'm nesting, but I'm not
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Validaiting emails with Net.Mail MailAddress
  • Fill an image in a square container while keeping aspect ratio
  • How to set my toolbar fixed while scrolling android
  • How to show dropdown in excel using jrxml (jasper api)?
  • Weird JavaScript statement, what does it mean?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Apache 2.4 - remove | delete | uninstall
  • AT Commands to Send SMS not working in Windows 8.1
  • Linker errors when using intrinsic function via function pointer
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Proper folder structure for lots of source files
  • Benchmarking RAM performance - UWP and C#
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app