Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listview - Physical attributes 2

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
I am using a Listview control as a listbox. I have two columns, the first contains record ID's and it's width is set to zero. The second column is filled with an item for selection. It all works however it looks a mess. I have been trying to adjust the width of the control in design view, but when in runtime it shows what looks like an unsed empty column. The heading text is narrower than the visible column. If I manually alter ther column width in runtime using the mouse it shows or hides the scrollbar. Is there any way I can make the control look like a normal listbox in design time. Thanks
 
Can you explain a little bit more? Are you saying you have three columns, but only wish two of them to be visible at runtime?
 
You could use the Tag property of the ListViewItem to hold your record ID.

The following assumes that the ListView's MultiSelect property has been set to False.

The For Loop is just to set a series of IDs that will be different from the Item Index.

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim lvi As ListViewItem
        For a As Integer = 1 To 20 Step 2
            lvi = New ListViewItem
            lvi.Tag = a
            lvi.Text = "Column: 1, Value: " & (a \ 2 + 1).ToString
            lvi.SubItems.Add("Column: 2, Value: " & (a \ 2 + 1).ToString)
            ListView1.Items.Add(lvi)
        Next

    End Sub

    Private Sub ListView1_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged

        If e.IsSelected Then Label1.Text = e.Item.Tag.ToString

    End Sub
 
Thanks. The listview is set to have only two columns, the first column is hidden (width=0) leaving the other column visible, however I am trying to remove the section to the right of the visible column which looks like a third column but I think it's just whitespace which the user can stretch the width of the visible column into. I might be wrong. Would not be so bad if it was the same colour as the column, just does not look very nice being there. I keep manally changing the width of the control in design mode to get an acceptable width, however having to allow width enough for a verticle scrollbar to appear when needed, it leaves the unwanted bit.
 
Thanks softhemc, could a standard listbox be used like that? I seem to have come across so many types of listboxes in my past, some of which offered itemdata where you could have the row items displayed in the listbox but you extracted the row itemdata when selecting a row. Thats the trouble for me, get to learn one application and another even though MS is totally different. I am trying to show a single row item to the user, and onclick extract a primary key number of the related record.
 
Thanks softhemc, could a standard listbox be used like that? I seem to have come across so many types of listboxes in my past, some of which offered itemdata where you could have the row items displayed in the listbox but you extracted the row itemdata when selecting a row. Thats the trouble for me, get to learn one application and another even though MS is totally different. I am trying to show a single row item to the user, and onclick extract a primary key number of the related record.

If that is your issue, then this completely changes things. In .Net, your DataBound controls Items collections contain objects. So if you bind a ListBox to a DataTable, it's Item collections will contain DataRowViews. You can access any column you want off of the DataRowView. You can still use things like .SelectedValue and .SelectedText, but you don't have to. You can access any related data you need.
 
You could use a DataGridView:

Code:
    Private MyDataTable As DataTable

    Private Sub CreateDataTable()

        MyDataTable = New DataTable
        Dim PK As DataColumn = New DataColumn("PK", GetType(Integer))
        Dim ValueColumn As DataColumn = New DataColumn("Value", GetType(String))
        MyDataTable.Columns.Add(PK)
        MyDataTable.Columns.Add(ValueColumn)

    End Sub

    Private Sub PopulateDateTable()

        For a As Integer = 1 To 20 Step 2
            Dim r As DataRow = MyDataTable.NewRow
            r("PK") = a
            r("Value") = "Column: 1, Value: " & (a \ 2 + 1).ToString
            MyDataTable.Rows.Add(r)
        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        CreateDataTable()
        PopulateDateTable()
        DataGridView1.DataSource = MyDataTable
        DataGridView1.Columns.Remove("PK")
        DataGridView1.Columns(0).Width = DataGridView1.ClientRectangle.Width - 2

    End Sub

    Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter

        Label1.Text = MyDataTable.Rows(e.RowIndex).Item("PK").ToString

    End Sub

You would probably also want to change some of the default values of various properties, especially:
[tt]
Set these to False:

AllowUserToAddRows
AllowUserToDeleteRows
AllowUserToResizeRows
ColumnHeadersVisible
RowHeadersVisible
MultiSelect


Set this to True:
ReadOnly


Set SelectionMode to FullRowSelect
Set CellBorderStyle to None
Set BackgroundColor to Window[/tt]
 
Thanks RiverGuy. I am filling the listbox :

MaxRows = ds.Tables(0).Rows.Count

' Fill a listbox
For ty = 0 To MaxRows - 1
ListBox1.Items.Add(ds.Tables(0).Rows(ty).Item(1))
Next

This is filling a single column. I wanted to add (have) two columns, first column an ID number, the second being an item to select. I only want the item displayed in the list and not the ID, but when a row is selected I want to get the ID number. Thanks again
 
No, see in your code, you are adding a String object to the Items collection of the ListBox. If you set it's DataSource property to your DataTable, you can access the other column values.

Here is some sample code:
Code:
            ListBox1.DataSource = ds.Tables(0)
            ListBox1.DisplayMember = "The name of your column to display"
            ListBox1.ValueMember = "The name of your column containing the ID"

'Display the ID for the selected item
            MessageBox.Show(CType(ListBox1.SelectedItem, DataRowView).Item("The name of your column containing the ID"))

'Display any other column's value for the selected item
            MessageBox.Show(CType(ListBox1.SelectedItem, DataRowView).Item("Some other column name"))
 
Perfect RiverGuy, just what I wanted, many thanks!!
 
i am doing something wrong here but cannot work it out.

I am trying to put the CType value into an SQL string

WHERE Company.ID1 = 'CType Value';", con)

All the variations I have tried result in an exception in the last line of the code saying it cannot find the column


Dim ds3 As DataSet = New DataSet()

Dim DataAdapter3 As MySqlDataAdapter = New MySqlDataAdapter()

DataAdapter3.SelectCommand = sql3

DataAdapter3.Fill(ds3, "temp1")

DataGridView3.DataSource = ds3

DataGridView3.DataMember = "Temp1
 
CType casts something to a type, so it doesn't go into a string. I was using it in my example because the .Items collection contains Objects. In order to call members of a DataRowView, we have to turn the Object into a DataRowView.
 
Thanks. I thought as I was able to put the value into a label.text I could convert it to a value or something. So does that mean I cannot use it in an SQL query as I wanted?
Regards
 
This is driving me up the wall. I tried this and get the same results

'get item and subitem values
If ListView1.SelectedItems.Count > 0 Then
i1 = ListView1.SelectedItems(0).SubItems(0).Text
i2 = ListView1.SelectedItems(0).SubItems(1).Text

Dim IJ As Integer = i1

Dim sql3 As MySqlCommand = New MySqlCommand("SELECT ....... WHERE Company.ID1 = 'IJ';", con)

Dim ds3 As DataSet = New DataSet()

Dim DataAdapter3 As MySqlDataAdapter = New MySqlDataAdapter()

DataAdapter3.SelectCommand = sql3

DataAdapter3.Fill(ds3, "temp1")

DataGridView3.DataSource = ds3

DataGridView3.DataMember = "Temp1"

I have tried both:
WHERE Company.ID1 = IJ;", con)
WHERE Company.ID1 = 'IJ';", con)

If I hard code a number in it works:
WHERE Company.ID1 = 20;", con)

I can see the number appear in a label.text so I know I am dealing with numbers or text so why can I not move forward with it. Thanks




 
Try:
[tt]
("SELECT ....... WHERE Company.ID1 = " & IJ.ToString() & ";", con)[/tt]

Or:
[tt]
("SELECT ....... WHERE Company.ID1 = '" & IJ.ToString() & "';", con)[/tt]
 
Hi Softhemc, I tried your first suggestion and IT WORKED!!
Thanks very much, I can now move on. Regards
 
Softhemc, I managed to use your idea for a listbox which was my preference as it looks tidier as a control. I used

Dim IJ As Integer = ListBox1.SelectedValue
("SELECT ....... WHERE Company.ID1 = '" & IJ.ToString() & "';", con)

And to my surprise it works, so thanks again
 
I had forgotten to add an explanation...

The first suggestion should work for numeric database columns, whereas the second should work for character type columns.

As Company.ID1 is an integer, the first suggestion works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top