Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
adding checkbox dynamically to the listview as sub-item in vb how do i do it ? my code is not working.. it create three checkbox at same place in listview. i want to add the checkbox at each row one by one..please help

What I have tried:

READER = command1.ExecuteReader

           While READER.Read()
               Dim item As New ListViewItem(READER.GetString("hos_id"))
               item.SubItems.Add(READER.GetString("name"))
               item.SubItems.Add(READER.GetString("room_no"))
               chk.Name = READER.GetInt32("hos_id")
               chk.Text = READER.GetInt32("hos_id")
               chk.Width = 70
               ListView1.Controls.Add(chk)
               AddHandler chk.CheckedChanged, AddressOf ChkBox_CheckedChanged
               ListView1.Items.Add(item)
           End While
Posted
Updated 16-Jan-18 21:51pm

1 solution

Hi, Prakash.

To get it right, because I don´t use Listviews.

The "item" should be an entry inside the Listview.

The item itself should contain "subitems" ?

Is it possible to use the Checkbox also as a Subitem?

So like this:
items.Subitems.add(chk) ???


and remove the line:
ListView1.Controls.Add(chk)


And if you need 3 checkboxes? (I don´t see three of them.)

Repeat the items.Subitems.add(chk) line with appropriate variablenames.

Maybe with a Loop inside the Readline.Read.

I would try to have only one Item which would contain the checkboxes and the all other stuff that is read out of a "Dataset" (the Data that are bundled to one Set).

So if the
Quote:
ListViewItem
can have Subitems and can have Controls inside of it, I would do all Data placed in Controls.
So like
Quote:
item.SubItems.Add(READER.GetString("name"))
item.SubItems.Add(READER.GetString("room_no"))


Will become for example:
item.Controls.Add(New Lable_xxx.Text = READER.GetString("name")
item.Controls.Add(New Lable_xxy.Text = READER.GetString("room_no")





If You are using WPF or UWP, please try to use Classes and Datatemplates.

Classes are the Data read out of the Reader into Objects.

These should go into an ObservableCollection (that is of Your Classtype)

then the ListView will be bound to this Collection (ItemsSource).

In Xaml the Datatemplate could look like this:

<DataTemplate x:Key="LindenstrasseTemplate"
                      DataType="local:LindenstrasseWikiPediaEpisode">
            <Border BorderBrush="Black"
                    BorderThickness="2"
                    CornerRadius="4"
                    d:DesignHeight="150"
                    d:DesignWidth="600">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <Label Content="{Binding Titel}"
                           Margin="4"
                           FontSize="36"
                           FontWeight="Bold" />
                    <Grid  Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="120" />
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <ProgressBar IsIndeterminate="{Binding Downloading, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                     Grid.ColumnSpan="2"
                                     Grid.Column="1"
                                     Height="28"
                                     Margin="4,0"
                                     Visibility="{Binding Downloading, Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <CheckBox Style="{StaticResource OrangeSwitchStyle}"
                                  IsChecked="{Binding DateiVorhanden, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Margin="10,0,4,0"
                                  Grid.Column="0"
                                  HorizontalAlignment="Left" />
                        <Label Grid.Column="1"
                               Content="{Binding StaffelFolge, FallbackValue=999}"
                               Width="Auto" />
                        <Label Content="{Binding Folge}"
                               Width="Auto"
                               Grid.Column="2" />
                        <Label Content="{Binding Sendedatum}"
                               HorizontalAlignment="Stretch"
                               Width="Auto"
                               Grid.Column="3" />
                    </Grid>
                    <Label Grid.Column="0"
                           Content="{Binding Dateiname}"
                           Margin="4"
                           Grid.Row="2" />

                </Grid>
            </Border>
        </DataTemplate>


The Listbox is my Example, but you can use the ListViewControl, and set the ItemsSource to the ObservableCollection, and also set the ItemTemplate, that the ListView can render the ListViewItems acoordingly.
("The SelectedItem to a Variable/Property to get all Information of the Selected (Dataset)" Only if You are using MVVM)

<ListBox x:Name="LBLindenstrasse"
                     VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                     AlternationCount="3"
                     Grid.Column="0"
                     ItemsSource="{Binding LindenstrasseListe.DieEpisoden, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                     ItemTemplate="{StaticResource LindenstrasseTemplate}"
                     HorizontalContentAlignment="Stretch"
                     SelectedItem="{Binding LindenstrasseAusgewählteEpisode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                     IsSynchronizedWithCurrentItem="True"
                     Background="#3F7742B5"
                     Grid.Row="1">



Please ask more about DataTemplates if needed.

c.u. Zui from Hamburg, who never used a ListView (hmm, maybe i should try one, sometime...)
 
Share this answer
 
Comments
CHill60 17-Jan-18 6:37am    
Hi Jaroslav, I deleted the other duplicated solutions based on your plea for help :-)
Jaroslav Mitrovic 17-Jan-18 6:52am    
thx. savior of the Universe lol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900