Solved

How to Bind Listview control in vb.net code

Posted on 2011-03-05
12
795 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

18 Experts available now in Live!

Get 1:1 Help Now