Solved

System.Data.DataRowView

Posted on 2006-06-21
14
680 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
[X]
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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

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!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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