Why Do I Get "System.Data.Datarowview" Instead of Real Values in My Winforms Listbox

Why do I get System.Data.DataRowView instead of real values in my WinForms Listbox?

I always have to deal with this problem, even if I set the DisplayMember and ValueMembers of the List Box.

Your current code is correct and should work, if you need access to the current selected item value of any column of your dTable you can get them doing this:

DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();

What I like about getting the entire DataRowView is that if you have more columns you can still access their values and do whatever you need with them.

Listbox returns System.Data.DataRowView instead of values

That code should work. I tried it with some test data and ListBox was filled with correct values.
To be sure, try to also set ValueMember like

listBox1.DisplayMember = "table_name";

I think the best approach is to add DataTable rows to your ListBox using loop or Linq list. After filling tabulusaraksts iterate through DataRows and add them as items to ListBox, without setting DataSource Something like this (Linq):

adapter.SelectCommand = listfill;
adapter.Fill(tabulusaraksts);
listBox1.Items.AddRange(tabulusaraksts.AsEnumerable().Select(row => row[0].ToString()).ToArray());
NpgsqlCommand showtable = new NpgsqlCommand("select * from " + tab +";" , ncon);
adapter2.SelectCommand = showtable;

or, using foreach loop

adapter.SelectCommand = listfill;
adapter.Fill(tabulusaraksts);

listBox1.Items.Clear();
foreach (DataRow row in tabulusaraksts.Rows)
{
listBox1.Items.add(tabulusaraksts[0].ToString());
}

NpgsqlCommand showtable = new NpgsqlCommand("select * from " + tab +";" , ncon);
adapter2.SelectCommand = showtable;

System.Data.DataRowView instead of real values

I guess chkdListBoxServers.DisplayMember = "server_name"; would be fine.

System.Data.DataRowView c#

The ListBox control takes each element of your DataTable and builds a elements for its Items property. But when it adds an element it doesn't know which one of your database fields should be showed in each of the strings added to the collection. In this case it uses the ToString method of the object used to add the elements. In your case is the ToString method of the DataRowView class. But the DataRowView class doesn't implement any particular ToString() method thus the base implementation (object.ToString()) is called and this returns simply the name of the class.

You could change this behavior using the properties

 listBox1.DataSource = t;
listBox1.DisplayMember = "datel";
listBox1.ValueMember = "number";

This will show just one column of your query. If you want to show more than one column you have two options. Ditch the ListBox control and use a DataGridView (there are a lot of examples in this site, use the search function) or create an alias in your query to concatenate together more than one field

String strDa = @"SELECT Lessons.number, Lessons.type, Lessons.datel, 
Lessons.hour, Lessons.money, Lessons.note,
Lessons.datel & ' ' & Lessons.hour & ' - ' & Lessons.money as LessonDetails
FROM Lessons";

....
listBox1.DisplayMember = "LessonDetails";

This is a bit clunky and produces a not very neat display. I recommend to use a DataGridView for this task

listbox selected item give me System.Data.DataRowView , C# winforms

How do you populate the listbox (ie what is exactly the datasource)?

From your comment I would say a DataView (and wich contains DataRowView...)

So you just need to cast the SelectedItem into DataRowView in order to get a value from this DataRowView:

foreach (object selectedItem in listBox1.SelectedItems)
{
DataRowView dr = (DataRowView)selectedItem;
String result = dr["productname"].ToString;
MessageBox.Show(result + Environment.NewLine);
}

The VB.Net developers that could fall on this post could also be interested in this.

System.Data.DataRowView, c#

You need to return the ID in your SELECT statement and assign the DataTable as the DataSource after you have filled the DataTable:

 SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);

You can then set up the ComboBox as such:

comboBox1.DataSource = dt.DefaultView;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name"

UPDATE

If you want to access the selected text or value of the item selected in the ComboBox, then you will need to do something like:

DataRowView drvItem = comboBox1.SelectedItem as DataRowView;

if (drvItem != null)
{
label4.Text = drvItem["ID"].ToString();
label3.Text = drvItem["Name"].ToString();
}

how to display value in listbox from dataview

You don't need to load data each time the TextChanged event fires. It's enough to load data in Load event of your form and then in TextChanged just filter the data. Then using an event like DoubleClick of ListBox set the text of TextBox:

private DataTable dataTable = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
string constring = @"Data Source=.;Initial Catalog=Test;User Id=sa;Password=admin@123";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT distinct * FROM Item_Details", con))
{
sda.Fill(dataTable);
}
}
this.listBox1.DataSource = new DataView(dataTable);
this.listBox1.DisplayMember = "IName";
this.listBox1.Visible = false;
}
private void txtIName_TextChanged(object sender, EventArgs e)
{
var dv = (DataView)this.listBox1.DataSource;
dv.RowFilter = string.Format("IName Like '%{0}%'", txtIName.Text);
listBox1.Visible = true;
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
var item = (DataRowView)listBox1.SelectedItem;
if (item != null)
this.txtIName.Text = item["IName"].ToString();
else
this.txtIName.Text = "";

this.listBox1.Visible = false;
}

Don't forget to attach Form1_Load, txtIName_TextChanged and listBox1_DoubleClick handlers to events.



Related Topics



Leave a reply



Submit