john
john

Reputation: 23

Checked items of checkbox of datagridview c#

I have added checkbox in datagridview and I want check whether an item is checked or not, and then read the content value, but I am little bit confuse how to accomplish it.
This is Xmal code

<DataGrid.Columns>
    <DataGridTemplateColumn Header="#">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox x:Name="checkboxinstance" Checked="checked_it" Unchecked="unchecked_it" content ="{Binding apiName }" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

This is behind the code:

private void checked_it(object sender, RoutedEventArgs e)
{
    List<CheckBox> checkBoxlist = new List<CheckBox>();
    foreach (CheckBox c in checkBoxlist)
    {
        //what I add here
    }
}

Upvotes: 0

Views: 193

Answers (1)

Fuzzybear
Fuzzybear

Reputation: 1418

You can use the IsChecked property to check if the checkbox has been ticked. To read the value of the Content you have to cast the type to a TextBlock

foreach (CheckBox c in checkBoxlist)
{
     If (c.IsChecked == true)
        {
        //Code when checkbox is checked
        var _tempTBL = (TextBlock) c.Content; //Get handle to TextBlock
        var foo = _tempTBL.Text; //Read TextBlock's text
        //foo is now a string of the checkbox's content
        }
}

MSDN link to the IsChecked property

MSDN link to the Content property

Upvotes: 3

Related Questions