Link to home
Start Free TrialLog in
Avatar of NCSO
NCSO

asked on

Listview to textbox

Hi Experts,

I am tired of punding my head in, so I am turning to the "guru's".  I have a listview that is populated at load from a database; this listview has five columns.  I want to (on click of checkbox) fill a textbox with the contents of column five of the selected record.

Example:

PersonType_ID        PersonNumber         Offense_Indicator          FullName                   person_id
       W                          W-1                          1,2,3                 Joe N. Smith                  99999


 
In the above example I need for the textbox to be filled with the 99999.  How can I make this happen?  Below is my current "LOAD" code for the form:



    Private Sub OffenseIncidnetPeopleEditAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GroupBox1.Text = "Offenses for Agency Report Number: **********"


        Dim conn2 As New System.Data.SqlClient.SqlConnection("Persist Security Info=True;Server=127.0.0.1;Database=Reporting;uid=sa;network=dbmssocn;password=xxxxxx")
        Dim da2 As New System.Data.SqlClient.SqlDataAdapter("Select * from tblPeople", conn2)
        Dim ds2 As New System.Data.DataSet
        Dim loggedOnUser As String
        loggedOnUser = My.User.Name
        Dim cmd2 As New System.Data.SqlClient.SqlCommand("select authorid from tblAuthor where username='" & loggedOnUser & "'", conn2)
        Dim cmd As New System.Data.SqlClient.SqlCommand("SELECT LastName +  ', ' + FirstName + ' ' + MiddleName AS FullName, PersonType_ID, Person_ID, PersonNumber, Offense_Indicator from tblPeople where arn='" & lblArn.Text & "' and Incident_ID='" & lblIncidentID.Text & "' and deleted='0' order by dateadded", conn2)

        Try
            conn2.Open()
            Dim myReader As SqlDataReader = cmd.ExecuteReader()
            Do While myReader.Read()

                Dim lvi As New ListViewItem(myReader.Item("PersonType_ID").ToString)
                lvi.SubItems.Add(myReader.Item("personnumber").ToString())
                lvi.SubItems.Add(myReader.Item("Offense_Indicator").ToString())
                lvi.SubItems.Add(myReader.Item("FullName").ToString())
                lvi.SubItems.Add(myReader.Item("person_id").ToString())

                ListView1.Items.Add(lvi)

            Loop

            myReader.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn2.Close()

        End Try
    End Sub
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Hi NCSO,
----------

you can try
TextBox1.Text =  ListView1.SelectedItems(0).SubItems(5).Text

not sure now if the subitems are starting at zero also so it could be
TextBox1.Text =  ListView1.SelectedItems(0).SubItems(4).Text

----------
bruintje
share what you know, learn what you don't
Avatar of NCSO
NCSO

ASKER

I have tried this and I get the following error:

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Private Sub ListView1_Click()

    my_SELECTED_ROW = ListView1.SelectedItem.Index'get current row
    LR= ListView1.ColumnHeaders.Count 'get the last col
    tmp = ListView1.ListItems(my_SELECTED_ROW).SubItems(LR - 1)
    Text1.Text = tmp
   
End Sub
you probably have not item selected on the first run so without selecting a row
selected items will be empty then you need to pass in a index yourself

TextBox1.Text =  ListView1.Items(0).SubItems(5).Text

Avatar of NCSO

ASKER

vb_elmer,

That didn't work either.  I forgot to mention, I am using VB2005
are you selecting an item in the load fucntion or not?
Avatar of NCSO

ASKER

no, at load the listview is filled from a database, I am selecting checkboxes
but i don't understand the sequence

1. you fill the listview
2. then click on a checkbox (in the listview?)
3. and the text box should show the subitemtext

or is there a step to select a row first
Avatar of NCSO

ASKER

Sorry,

1. Form loads
2. onLoad the listview fills
3. click checkbox on listview
4. textbox show the data in column 5
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>  I forgot to mention, I am using VB2005
uaahhh .
The DataGridView control in vb2005 is excellent and far superior to the old datagrid of .net1.x and may be a better and easier replacement to your listbox.

Then all youd need to do would be to load the dataset set using a dataadaptor instead of a command reader and Bind it to the DataGridView (the DataGridView is easy to set as readonly or additional behavioru for each column action unlike the old DataGrid)  (actuall recommed binding it via a DataView control for easy readonly setting) and catch the currentcellchanged event and check if is column 4 (5-1) and react accordingly...
oh youd probably want to set RowselectMode = true to make it look a little more like a list box (and even more so if you removed the side and top headers and sett the gridcolumn colour to systemcolor.window)