0

I am working on a WPF windows application and I need a help to solve some problem in it.

I have a data gridview; here is a XML:

<DataGrid x:Name="MyDataGrid" Margin="0,55.333,8,209.692" Width="273.27">
 <DataGrid.Columns>
                <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding ID}" Visibility="Hidden" />
                <DataGridTextColumn Header="CopID" IsReadOnly="True" Binding="{Binding CopID}" Visibility="Hidden"/>
                <DataGridTextColumn Header="CgID" IsReadOnly="True" Binding="{Binding CgID}" Visibility="Hidden"/>
                <DataGridTextColumn Header="Item Names" Width="190" IsReadOnly="True" Binding="{Binding ItemName}"/>
                <DataGridCheckBoxColumn  Header="Select" CanUserReorder="False" />
            </DataGrid.Columns>
</DataGrid>

I want to check DataGridCheckBoxColumn CheckBox is true or false in loop My hope is this to read a Column Value when user click on button; here is the code:

for (int i = 0; i < MyDataGrid.Items.Count - 1; i++)
{
    /*Some code here for Check CheckBox True or False*/
    for (int j = 0; j < MyDataGrid.Columns.Count; j++)
    {      
      string Massage = (MyDataGrid.Items[j] as DataRowView).Row.ItemArray[3].ToString();
      MessageBox.Show( Massage);
     }
 }

I want to check CheckBox is true or false in the loop to get value of that row Columns where is CheckBox is checked I hope somebody can help me to compete my code which I have done give up. I want this code to work when user click on button because I have to save record with this loop in database where CheckBox is true in MyDataGrid.

And I also want to select all check box of datagrid when I will check the CheckBox call Select All. I hope some buddy help me in it as soon as possible Thank You

1 Answer 1

1

I'll start by saying that typically, you shouldn't do that, and that's why it's more difficult then you expect. You should usually access the underlying DataSource instead of the Cells in the DataGrid itself.

That said, it is possible. For example:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
        const int CHECKBOX_COLUMN = 4;
        for (int i = 0; i < MyDataGrid.Items.Count - 1; i++)
        {

            DataGridCell cell = GetCell(i, CHECKBOX_COLUMN);
            CheckBox tb = cell.Content as CheckBox;
            MessageBox.Show(tb.IsChecked.ToString());
        }
}

Where the function GetCell() (and the inner functions that GetCell uses) can be found here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Blachshma it is really help full for me. and can you please tel me know how can if i want to select all checkboox and deselect all checkbook using select all button or using checkboox is that same code will be help full for it or have to do different code for that?
Once you have the CheckBox you can manually check or uncheck it, so the code is very similar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.