Read from Listbox = System.Data.DataRowView

TheWizardofInt

Junior Contributor
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
I write to a listbox using a datavew of an Access table

I need to be able to read from the listbox programatically and unencrypt the data

This code:
Code:
     Dim s As String
        If cmbName.SelectedIndex >= 0 Then
            s = lstLogin.SelectedItem.ToString
            txtLogin.Text = s
        End If
Returns "System.Data.DataRowView". It appears that I have to create a datarow to read the information. Anyone know the final step to this?
 
As it turns out, you just don't do it this way

What I ended up doing is, when I pulled the info from the recordset, I moved it to a table and left the table open in the program. When I wanted to pull info from the list box, I got its SelectedItemIndex and used it to pull the datarow from the table.

Works super vast, very low resources requirement and it made updating a lot easier.
 
This is a bug yet not fixed by Microsoft...

If you have a Listbox with Databindind this happens :(.

What you have to do is to put all the bindings properties before the datasource! EVen when it's generated automaticly, expand the #Region " Windows Form Designer generated code " and just change the order of the databinding properties because it always puts the datasource property in first place... make it the last one and it will work just fine...

Your work around works fine but with databinding is way more "clean" !


:)
 
:D... VS.net 2002 had some errors one of them is this one...
I undestand that VS its a way too complicated package to develop, witch can generate this kind of errors but, and specially this one, I think could ha been fixed on the 2003 version.

This is just me thinking...:o
 
i still can't get this to work.

i expanded the auto-coded portion as u said, and placed the datasource at the end, but still get System.Data.DataRowView.

wat im doin is using an OwnerDrawFixed draw mode. my data source is a dataset. the reason don't need the currently selected item, but rather the item that the listbox is trying to draw.

for the life of me, i can't figure out how to simply get the text for the listbox item it wants to draw.

my component initialization looks like this (with your suggested change *if i did it rite*) :

this.listBox1.DisplayMember = "firstname";
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.listBox1.Location = new System.Drawing.Point(16, 16);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(160, 238);
this.listBox1.TabIndex = 4;
this.listBox1.Tag = "";
this.listBox1.ValueMember = "mateid";
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged_1);
this.listBox1.DataSource = this.buddiesData11.mates;



-------------

and my Draw function (with the drawing removed while i debug) looks like this....

private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Font myFont;
myFont = new System.Drawing.Font("Comic Sans", 11);
int itemIndex = e.Index;
//debug purposes
String WHY = this.listBox1.Items[itemIndex].ToString();
String ME = this.listBox1.Items[itemIndex].ToString();

}
}



hopefully thats not too mangled... but both "WHY" and "ME" are System.Data.DataRowView

help? :confused:
 
oops. WHY and ME were the same above.

WHY is supposed to be:

this.listBox1.SelectedItem.ToString()

it is still "System.Data.DataRowView"

thx for any tips u can provide.
 
It would appear the problem is that you are adding a bunch of DataRowView objects to the listbox, and DataRowView's don't have an overloaded ToString() method, so it just returns the name of the type. Since ListBoxes use the ToString() method to tell what string to display for each item, it will always return DataRowView.

I would suggest that you try adding the data to the ListBox manually, so you can control exactly what is displayed.
 
thanks for the reply.

ok, just to make sure this is ok....

my datasource is a dataset which is filled via web services method and on back end is sql to back end db.

so, the listbox is notified to update itself when this dataset changes

so your saying, intervene there and override that method within the listbox to update it manually myself from the datasource?

im newbie at windows programming so appreciate your patience here.

thx....
 
It's stupid, I know, but try this...

[CS]

this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.listBox1.Location = new System.Drawing.Point(16, 16);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(160, 238);
this.listBox1.TabIndex = 4;
this.listBox1.Tag = "";
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged_1);

this.listBox1.ValueMember = "mateid";
this.listBox1.DisplayMember = "firstname";
this.listbox1.DataMember = mates;
this.listBox1.DataSource = this.buddiesData11;
[/CS]

I just seperate the DataSource from the DataMember got it? (the 2 last lines)...
 
Other good practice it's to use DataViews instead of pick the data directly from de DataSet.

If you use DataView you can, for example, filter your data...



:D Try the code above...
 
thx again. i tried the above code and i wasn't able to compile that as 'System.Windows.Forms.ListBox' does not contain a definition for 'DataMember'. looking it up, it seems DataMember is used to bind the ToolbarDropDownList control.
im going to dig deeper here to get a better understanding of what you are saying. cheers.
 
I'm really sorry for that...

I made it from my mind and it really doesn't exists! :(

After this I don't know what to tell u more!
Try to fill the dataset before asigning the binding properties of the ListBox... :D
 
just an fyi. what i am doing to get around this is extract the field from the datarow view itself.


private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Font myFont;
myFont = new System.Drawing.Font("Comic Sans", 11);


DataRowView drv = (DataRowView)this.listBox1.Items[e.Index];
String s = drv["firstname"].ToString();


s results with the listbox item currently being drawn.

cheers
 
Dim drv As DataRowView
Dim dr As DataRow

tmp = combo1.SelectedIndex
drv = CType(combo1.SelectedItem, DataRowView)
dr = drv.Row
tmpstr= dr.Item(0)

'tmpstr will return the selected item
 
Solution

Hi all, im very glad I came across this site, it enlightened me to how to overcome the problem :)

what i did was to populate a dataview with the datasets data and use that as the datasource.

works fine now :)
 
Clarification

VolteFace said:
It would appear the problem is that you are adding a bunch of DataRowView objects to the listbox, and DataRowView's don't have an overloaded ToString() method, so it just returns the name of the type. Since ListBoxes use the ToString() method to tell what string to display for each item, it will always return DataRowView.

I would suggest that you try adding the data to the ListBox manually, so you can control exactly what is displayed.

VolteFace,

Paraphrasing: If you want text from a listbox, then just add text to the listbox or objects that have a good old-fashioned ToString()?

I have seen this listbox problem many times before and have had a hunch as to what to do, but your post simplifies the answer. :cool:
 
On the same topic - I am seeing something I haven't seen before.

I build a dropdown of states, and use values with the 2 letter abbreviations

You select the state, the program reads the values

In the test environment, this works. Post it to the web site and it throws an error, there is no method for get value

Anyone seen that before?
 
Back
Top