Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

System.Data.DataRowView

Posted on 2006-06-21
14
Medium Priority
?
695 Views
Last Modified: 2012-08-13
Hi

I do this and it seems to work fine.

private void populateListBox(ListBox listBoxToPopulate, string sqlQuery)
        {
            //populate listbox1
            SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = "Data Source=PC001;Initial Catalog=mytest;Integrated Security=True;";
            sqlConn.Open();

            SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConn);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            DataTable dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);

            listBoxToPopulate.DataSource = dataTable;
            listBoxToPopulate.DisplayMember = "website";
            listBoxToPopulate.ValueMember = "uid";

But when I try to do (move a selected item from one listbox to another)

I get

if (MessageBox.Show("Are you sure you want to move the website " + listBox1.SelectedItem.ToString() + "?", "Confirm Move", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

Are you sure you want to move the website System.Data.DataRowView

Even though my listBox is showing
www.1.com
www.2.com
www.3.com

Thanks

0
Comment
Question by:boofulls
  • 6
  • 6
  • 2
14 Comments
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16950223
Hi boofulls,

What does 'SelectedValue' get you? (if it even works!)

Cheers

Wint
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 16950229
You need to use listBox.SelectedItem.Text in your MessageBox.Show()
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16950243
Oh yes,
That's much better :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:boofulls
ID: 16950248
Wint

Selected Value gives me the index of the website.

It does change to the correct index alright!

Now just to get at the actual value that its displaying!
0
 

Author Comment

by:boofulls
ID: 16950255
code now

if (MessageBox.Show("Are you sure you want to move the website " + listBox2.SelectedItem.Text + "?", "Confirm Move", MessageBoxButtons.YesNo) == DialogResult.Yes)
           

Error      1      'object' does not contain a definition for 'Text'      
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16950273
You could cast it to a DataRowView?

((DataRowView)listBox2.SelectedItem).Item.ToString()

See if that works?

Wint.
0
 

Author Comment

by:boofulls
ID: 16950282
if (MessageBox.Show("Are you sure you want to move the website " + ((DataRowView)listBox2.SelectedItem).Item.ToString() + "?", "Confirm Move", MessageBoxButtons.YesNo) == DialogResult.Yes)
           

Error      1      'System.Data.DataRowView' does not contain a definition for 'Item'
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16950285
or that may need to be:

  ((DataRowView)listBox2.SelectedItem)["website"].ToString();

whooops.....
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 16950296
Try this:
((DataRowView)listBox2.SelectedItem)["website"]
0
 

Author Comment

by:boofulls
ID: 16950304
Great Wint that worked.

But now I get a crash on my next line.

if (MessageBox.Show("Are you sure you want to move the website " + ((DataRowView)listBox2.SelectedItem)["website"].ToString() + "?", "Confirm Move", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                //add to listbox1
                listBox1.Items.Add(listBox2.SelectedItem);
                //remove from listbox2
                listBox2.Items.Remove(listBox2.SelectedItem);


I always want to keep the 'key' and 'value' for every item in a listbox.

How would I do the add and remove?

Thanks!
0
 

Author Comment

by:boofulls
ID: 16950340
Also if I try to do a Remove I get a crash

if (MessageBox.Show("Are you sure you want to delete the website " + listBox2.SelectedItem.ToString() + "?", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                listBox2.Items.Remove(listBox2.SelectedItem);
                button3.Enabled = true;
            }



Items collection cannot be modified when the DataSource property is set.
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16950355
I presume you've always had this crash? It's not caused by the code in the 'if' statement, so it kind of depends on what you are wanting to put in to the other listbox.

I think you'd need to have two datasets to 'move' between one listbox and another, and you'd have to delete from the datasets rather than the listboxes themselves.

Personally I might have converted my database read into a dictionary / hashtable and used that as my back end, with a uid |-> url setup. Then just add the urls to each listBox, and when I come to need the uid pick it up from the backend dictionary.

In answer to your second bit (Removing), I assume this is because you are trying to use the listbox to edit the dataset, where you should be editing the dataset directly. I think that changes in that would then be propogated to the listbox.


Wint.
0
 

Author Comment

by:boofulls
ID: 16950373
Thanks Wint.

So it would be better to use a hashtable than have hidden values for each value in the listbox?

I thought that adding hashtables might be less efficient than just adding that data to the listbox.

Cheers
0
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 200 total points
ID: 16950385
Well, it's the way I would do it purely because that's the way I tend to deal with things like that. I'm not sure if it's the best way or not, but it's the route I would take.

You can have one backend Hashtable for both listboxes if you want, so you don't need one per listbox.

Wint.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question