Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
I have listbox with checked box as following, & it binding its data from sql server database,
, I want to get selected items value When I run this but I got this error
 
Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Controls.CheckBox'.
 

<Window.Resources>
        <DataTemplate x:Key="NameColumnTemplate">
            <CheckBox Height="20"  FontFamily="Arial" FontSize="14"  Content="{Binding Path=PermissionDescription}" 
                      Tag="{Binding PermissionID}" HorizontalAlignment="Stretch" VerticalAlignment="Center"
                      />
        </DataTemplate>
    </Window.Resources>
<Grid>
<ListBox HorizontalAlignment="Stretch" Margin="12,12,136,21" Name="lstEmployees" 
                          VerticalAlignment="Stretch" ItemsSource="{Binding Tables[0]}"  
                          ItemTemplate="{StaticResource NameColumnTemplate}" 
                          ScrollViewer.VerticalScrollBarVisibility="Auto" removed="{x:Null}" 
                         BorderBrush="#FFAD7F30"  
                 SelectionChanged="lst_SelectionChanged" CheckBox.Click="lst_SelectionChanged"/>
<Button Content="listbox" Height="23" HorizontalAlignment="Left" Margin="214,207,0,0" Name="btnShowSelectedItems" 
                VerticalAlignment="Top" Width="75" Click="btnShowSelectedItems_Click" />
</Grid>
 
 public Window2()
        {
            InitializeComponent();
            // bind data 
            lstEmployees.DataContext = SelJobsCat();
        }
 
 private void btnShowSelectedItems_Click(object sender, RoutedEventArgs e)
        {
            foreach (CheckBox item in lstEmployees.Items)
            {
                if (item.IsChecked == true)
                {
                    System.Windows.MessageBox.Show((item.Content + " is checked."));
 
                }
            }
        }
 
        private void lst_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is CheckBox)
            {
                lstEmployees.SelectedItem = e.OriginalSource;
            }
 
            if (lstEmployees.SelectedItem == null) return;
            Console.WriteLine(lstEmployees.SelectedIndex);
            Console.WriteLine(((CheckBox)lstEmployees.SelectedItem).IsChecked);
        }
 
 
where is my error please, Thanks
Posted 29-Jun-13 6:19am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the CheckBoxList's GetItemChecked or GetItemCheckState method
 
for (int i = 0; i < lstEmployees.Items.Count; i++)
  if (clbIncludes.GetItemChecked(i))
    System.Windows.MessageBox.Show((item.Content + " is checked."));
  else
    // Do unselected stuff

or if you want to only include actually checked items:
 
if (stEmployees.GetItemCheckState(i) == CheckState.Checked)
 
or listitem.selected:
 
foreach (ListItem listItem in lstEmployees.Items)
{
    if (listItem.Selected) {
        //do some work
    }
    else {
        //do some work
    }
}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Maciej Los 250
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 29 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100