?
Solved

ListView Checkbox Column Data Value?

Posted on 2007-08-09
6
Medium Priority
?
532 Views
Last Modified: 2013-11-05
I'm wondering what a ListView is good for other than simply displaying data.

The CheckBox in the first column looks nice, but how can I save its changed value back to the dataset?

So, back to my original question.

Q. How do I get the data 'value' of the the column, not the true/false checkbox value when I check or uncheck a box?

It would have been so much better if MS designed the CheckBox in it's own column.

 
0
Comment
Question by:kvnsdr
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19668610
Unfortunately, all you can do is loop the items:

 DataTable dt = new DataTable("Items")
 foreach (ListViewItem item in listView1.Items
 {
     DataRow dr = dt.NewRow();
     dr["Name"] = item.Text;
     dr["IsSelected"] = item.Checked;
     dt.Rows.Add(dr);
 }

Bob
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 19668718
How do i save all items and subitems to a dataset/datatable?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19669470
Example is for ListViewItems, you can easily extend that to include sub items also.  Where are you having a problem?

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:kvnsdr
ID: 19674079
Including sub items.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 19674369
Okay, If possible avoid the dataset and update database directly like so.

I almost have it figured out, but get an error:

"An expression of non-boolean type specified in a context where a condition is expected, near')'

I included the message box to view the 'checked' value, it says 'True'

Table has three columns and pri key (col1 int, col2 varchar, col3 bit)

This SQL Query works: UPDATE table1 SET col3 = 1 WHERE col1 = '99' -- col1 used for testing Update syntax only

[Code]

 cn = new SqlConnection(cnPath);
 SqlCommand cmd = cn.CreateCommand();
 cn.Open();

 foreach (ListViewItem item in listView1.Items)
            {
                MessageBox.Show(listView1.Items[0].Checked.ToString());

                cmd.CommandText =
               "UPDATE table1 SET col3 = 1 WHERE '" + listView1.Items[0].Checked.ToString() + "') ";
               
               cmd.ExecuteNonQuery();
             }
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 19674431
I don't see in this SQL:

     UPDATE table1 SET col3 = 1 WHERE '" + listView1.Items[0].Checked.ToString() + "') "

where you have a column for the WHERE:
   UPDATE table1
      SET col3 = 1
      WHERE id = @id

Bob
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

807 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