Solved

How to Bind Listview control in vb.net code

Posted on 2011-03-05
12
794 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 17

Expert Comment

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

Expert Comment

by:nepaluz
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:mgmhicks
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did that last suggestion fix cellstyle application issue too?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now