Silverlight / WPF - how to get chech box checked or not in datagrid

Asked By sushant kumar kesari on 26-Dec-11 04:25 AM
hello frnd

how to get chech box checked or not in datagrid .

i want info to when i change the the checked or uncheked of check box in datagrid,.
how can i do this operations  and which event.
Jitendra Faye replied to sushant kumar kesari on 26-Dec-11 04:31 AM
Try like this-

  CheckBox chk = myDataGrid.Columns[0].GetCellContent(e.Row) as CheckBox;
    if( chk.IsChecked ==true)
    {
    //Your code
    }



Try this and let me know.
Riley K replied to sushant kumar kesari on 26-Dec-11 04:34 AM

You have to use datagridcheckboxcolumn like thi s


<data:DataGrid Name="myGrid" AutoGenerateColumns="False">
 
  <data:DataGrid.Columns>
 
    <data:DataGridCheckBoxColumn Binding="{Binding Chosen, Mode=TwoWay}" />
    <data:DataGridTextColumn Binding="{Binding Name}" />
 
  </data:DataGrid.Columns>
 
</data:DataGrid>


Refer this link

http://forums.silverlight.net/t/36172.aspx/1?Datagrid+with+check+box

Regards
Riley K replied to sushant kumar kesari on 26-Dec-11 04:43 AM

Create a method for checkbox 

<CheckBox x:Name="chkSelectAll" Click="chk_Click"  Margin="2,0,0,0" Content="Select" d:LayoutOverrides="Width, Height" VerticalAlignment="Center" HorizontalAlignment="Left"/>

the event

void chk_Click(object sender, RoutedEventArgs e)
{
  CheckBox chk = sender as CheckBox;
  bool check = chk.IsChecked.Value;
  if (check)
  {
     // Your code
  }
  else
  {
    foreach (Person p in myList)
    {
       // your code
    }
      
  }
}

Regards