Link to home
Start Free TrialLog in
Avatar of mgmhicks

asked on

How to Bind Listview control in code

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

Avatar of nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of mgmhicks


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.

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
            ' 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)
                If mDS.Tables(1).Rows(i).Item(6) = True Then
                    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))
                End If

                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 = 

        End If

    End Function

Open in new window

I'll get back to you in a while (hopefully before the ystem goes down for maintanance)
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
Avatar of nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"

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.
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, 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
            mCHG = "C"
        End If

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


        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
        End If

    End Function

Open in new window

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.
Saved me alot of time trying to bind listview when datagridview was the way to go all along.
Did that last suggestion fix cellstyle application issue too?