Solved

How to Bind Listview control in vb.net code

Posted on 2011-03-05
12
803 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
[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
  • 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
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.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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