Solved

How to Bind Listview control in vb.net code

Posted on 2011-03-05
12
800 Views
Last Modified: 2012-05-11
I have a xml file I read into a dataset.  I have a listview control with about 5 columns.  So when someone chooses a work order, I find the record if it exists I fill the listview with items.  Now the items can be deleted or more items could be added, so I though lets bind the data so any changes made to the listview will be updated in the dataset.  Then I can do dataset.update and write the xml.  However I can fill the listview, however this is not binding it and I am wondering if this can even be done with the listview control

thanks
0
Comment
Question by:mgmhicks
  • 7
  • 5
12 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35044266
You will have to terate the dataset for the items and add them as usual to the listview.

The key to the updating is to write code in the handlers (textchanged??) to update the dataset.

Another method is to write your own custom listview (inherited from the generic listview) and add the necessary datasource code.
0
 

Author Comment

by:mgmhicks
ID: 35044291
Here is the code I use, can I get a example of what of what you are saying.  Will I be able to tell if the textchanged, or if it was deleted or another row added.  Then update the dataset accordingly, by replace dataset items with new values and do a dataset.update.  Not sure though about going through the dataset to find the lines that have changed.

thanks
0
 

Author Comment

by:mgmhicks
ID: 35044293
Sorry here it is
Private Function BindDataSet(ByVal myWO As String, ByRef mDS As DataSet) As Boolean
        Dim i As Integer = 0
        mDS.Tables(0).DefaultView.RowFilter = "WO_Number = '" & myWO & "'"
        If mDS.Tables(0).DefaultView.Count = 0 Then
        Else
            ' we have rows to bind
            mDS.Tables(1).DefaultView.RowFilter = "WO_Number = '" & myWO & "'"
            For i = 0 To mDS.Tables(1).DefaultView.Count - 1
                Dim myObj As New ListViewItem
                Dim curRow As DataRow
                curRow = mDS.Tables(1).Rows(i)
                myObj.Text = mDS.Tables(1).Rows(i).Item(2).ToString
                ' myObj.SubItems.Add(mDS.Tables(1).DefaultView.Item(3).ToString)
                myObj.SubItems.Add(mDS.Tables(1).Rows(i).Item(5).ToString)
                myObj.SubItems.Add(mDS.Tables(1).Rows(i).Item(4).ToString)
                If mDS.Tables(1).Rows(i).Item(6) = True Then
                    myObj.SubItems.Add("R")
                    myObj.ForeColor = Color.Red
                    lblResiCharges.Text = FormatCurrency(CDbl(lblResiCharges.Text) + (CDbl(mDS.Tables(1).Rows(i).Item(5).ToString)) * CDbl(mDS.Tables(1).Rows(i).Item(4).ToString))
                Else
                    myObj.SubItems.Add("C")
                End If

                myObj.SubItems.Add(mDS.Tables(1).Rows(i).Item(1).ToString)
                lblTotalCharges.Text = FormatCurrency(CDbl(lblTotalCharges.Text) + (CDbl(mDS.Tables(1).Rows(i).Item(5).ToString)) * CDbl(mDS.Tables(1).Rows(i).Item(4).ToString))
                ' myObj.Text = 
                ListView1.Items.Add(myObj)
            Next

        End If

    End Function

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 17

Expert Comment

by:nepaluz
ID: 35044393
I'll get back to you in a while (hopefully before the ystem goes down for maintanance)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35044538
I hadn't read your response omprehensively before the system went dow.

For code suggestion (and if you are not avert to a bit of reading), you can implement databinding by reading here http://www.codeproject.com/KB/list/ListView_DataBinding.aspx
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 35044553
Honestly, aside from gaining more knowledge from implementing the databindings of a listview, I'd suggest you replace your listview with the DataGridView (or GridView in asp). All the functionality that you require comes pre-packaged in the GridView, whereas with the listview (and your specific scenario) it feels more like re-inventing the wheel! (for my twopence)
0
 

Author Comment

by:mgmhicks
ID: 35046359
Ok, I agree with you and the idea of the datagridview.  I've gotten it to look like the listview.  However when I would add a item to the datagridview, a new row, how do I get a row object from the datagridview without any rows actually in it.

dim myRow as new datagridviewrow
myrow = dgItems.?????
myrow.cells(0).value = "1"
myrow.cells(1).value = "2"

thanks
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35046414
I don't seem to understand your question. Are you loking to add a new item to the table / grid?

I was of the assumption that you are going to bind your datatable to the datagridview, thus automaically have it populated. Any changes made on the datagridview can be automatically persisted to the datatable.
0
 

Author Comment

by:mgmhicks
ID: 35046675
Ok, heres what I have seems to be working great.  I bind the dataset to the grid when adding and editing the items in the grid.   So I am accepting your grid solution, much easier and less code using the grid.  However please look below and let me know why when I have the gridrow color.red, when I add another line that needs to be red, the previous line or record is no longer red, just the last line.  I have a issue finding the gridrow index of the newly added row.

thank you again very much.

Private Function FillListBox(ByVal mText As String, ByVal mTag As String, ByVal mQty As Integer, ByVal mResident As Boolean) As Boolean
        Dim mPrice As String = SplitString(mTag, 1)
        Dim mItemID As String = SplitString(mTag, 0)
        mInspectionCost = mInspectionCost + (CDbl(mPrice))
        Dim myGroup As String = TabInspection.SelectedTab.Text
        Dim mCHG As String = ""
        Dim myObj As New ListViewItem

        Dim myRow As DataRow
        myRow = dsInspectionsInv.Tables(1).NewRow
        myRow.Item(0) = Trim(lblWONo.Text)
        myRow.Item(1) = mItemID
        myRow.Item(2) = Trim(myGroup)

        lblTotalCharges.Text = FormatCurrency(CDbl(lblTotalCharges.Text) + (CDbl(mPrice) * CDbl(mQty)))

        If mResident = True Then
            mCHG = "R"
            lblResiCharges.Text = FormatCurrency(CDbl(lblResiCharges.Text) + (CDbl(mPrice) * CDbl(mQty)))
            '  myRow.Cells. = Color.Red
        Else
            mCHG = "C"
        End If

        myRow.Item(3) = Trim(mText)
        myRow.Item(4) = mQty
        myRow.Item(5) = mPrice
        myRow.Item(6) = mCHG

        dsInspectionsInv.Tables(1).Rows.Add(myRow)
        dsInspectionsInv.AcceptChanges()

        lblTotalCharges.Text = FormatCurrency(lblTotalCharges.Text, 2)
        lblResiCharges.Text = FormatCurrency(lblResiCharges.Text, 2)
        If mResident = True Then
            dgItems.Rows(dgItems.Rows.Count - 1).DefaultCellStyle.ForeColor = Color.Red
        Else
        End If

        MsgBox(dsInspectionsInv.HasChanges)
    End Function

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35046725
After line 31 of your code above (I assume you are accepting changes to your table), can you immediately after refresh the datagridview too? It seems by the time you call the cellstyle (on line 36) the changes have not yet percolated through.
0
 

Author Closing Comment

by:mgmhicks
ID: 35046749
Saved me alot of time trying to bind listview when datagridview was the way to go all along.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35046848
Did that last suggestion fix cellstyle application issue too?
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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