Solved

System.Data.DataRowView

Posted on 2006-06-21
14
675 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
orderby list (from Json) 1 44
Need help with a query 14 54
Windows Universal App 22 63
I'm looking for a dynamic length jQuery/javascript currency editor mask? 2 65
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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