Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

System.Data.DataRowView

Posted on 2006-06-21
14
666 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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 50 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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