Solved

System.Data.DataRowView

Posted on 2006-06-21
14
649 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now