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 OffenseIncidnetPeopleEditA dd_Load(By Val 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.SqlC onnection( "Persist Security Info=True;Server=127.0.0.1 ;Database= Reporting; uid=sa;net work=dbmss ocn;passwo rd=xxxxxx" )
Dim da2 As New System.Data.SqlClient.SqlD ataAdapter ("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.SqlC ommand("se lect authorid from tblAuthor where username='" & loggedOnUser & "'", conn2)
Dim cmd As New System.Data.SqlClient.SqlC ommand("SE LECT 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 ("PersonTy pe_ID").To String)
lvi.SubItems.Add(myReader. Item("pers onnumber") .ToString( ))
lvi.SubItems.Add(myReader. Item("Offe nse_Indica tor").ToSt ring())
lvi.SubItems.Add(myReader. Item("Full Name").ToS tring())
lvi.SubItems.Add(myReader. Item("pers on_id").To String())
ListView1.Items.Add(lvi)
Loop
myReader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message )
Finally
conn2.Close()
End Try
End Sub
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 OffenseIncidnetPeopleEditA
GroupBox1.Text = "Offenses for Agency Report Number: **********"
Dim conn2 As New System.Data.SqlClient.SqlC
Dim da2 As New System.Data.SqlClient.SqlD
Dim ds2 As New System.Data.DataSet
Dim loggedOnUser As String
loggedOnUser = My.User.Name
Dim cmd2 As New System.Data.SqlClient.SqlC
Dim cmd As New System.Data.SqlClient.SqlC
Try
conn2.Open()
Dim myReader As SqlDataReader = cmd.ExecuteReader()
Do While myReader.Read()
Dim lvi As New ListViewItem(myReader.Item
lvi.SubItems.Add(myReader.
lvi.SubItems.Add(myReader.
lvi.SubItems.Add(myReader.
lvi.SubItems.Add(myReader.
ListView1.Items.Add(lvi)
Loop
myReader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message
Finally
conn2.Close()
End Try
End Sub
ASKER
I have tried this and I get the following error:
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Private Sub ListView1_Click()
my_SELECTED_ROW = ListView1.SelectedItem.Ind ex'get current row
LR= ListView1.ColumnHeaders.Co unt 'get the last col
tmp = ListView1.ListItems(my_SEL ECTED_ROW) .SubItems( LR - 1)
Text1.Text = tmp
End Sub
my_SELECTED_ROW = ListView1.SelectedItem.Ind
LR= ListView1.ColumnHeaders.Co
tmp = ListView1.ListItems(my_SEL
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).SubItem s(5).Text
selected items will be empty then you need to pass in a index yourself
TextBox1.Text = ListView1.Items(0).SubItem
ASKER
vb_elmer,
That didn't work either. I forgot to mention, I am using VB2005
That didn't work either. I forgot to mention, I am using VB2005
are you selecting an item in the load fucntion or not?
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
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
ASKER
Sorry,
1. Form loads
2. onLoad the listview fills
3. click checkbox on listview
4. textbox show the data in column 5
1. Form loads
2. onLoad the listview fills
3. click checkbox on listview
4. textbox show the data in column 5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> I forgot to mention, I am using VB2005
uaahhh .
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...
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)
----------
you can try
TextBox1.Text = ListView1.SelectedItems(0)
not sure now if the subitems are starting at zero also so it could be
TextBox1.Text = ListView1.SelectedItems(0)
----------
bruintje
share what you know, learn what you don't