delete row by enabling checkbox in datagrid in Uwp

Javier R 211 Reputation points
2022-01-24T11:31:38.433+00:00

Hello:
I want to delete the row from the datagrid in the table. when you activate checkbox and then press the button to clear the record from the table. When the CheckBox is not activated, it cannot be deleted.

 private async void AppDelete_Click(object sender, RoutedEventArgs e)
        {

            try
            {

                const string SqlString = "Delete  From PetTable where ID= @IdPet";



                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();

                        if (conn.State == System.Data.ConnectionState.Open)
                        {
                            using (SqlCommand cmd = conn.CreateCommand())
                            {

                                cmd.CommandText = SqlString;
                                cmd.Parameters.Clear();


                                cmd.Parameters.Add("@IdPet", SqlDbType.Int);

                                cmd.ExecuteNonQuery();
                                conn.Close();



                                MessageDialog Md = new MessageDialog("The record has been inserted");
                                await Md.ShowAsync();
                            }


                        }
                    }

            }
            catch (Exception ex)
            {
                var Msg = new MessageDialog("Exception:" + ex.Message);
                Msg.Commands.Add(new UICommand("Close"));
            }



         }

//xaml Datagrid

                    <controls:DataGridTemplateColumn x:Uid="Select_Header"   MaxWidth="100">
                        <controls:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox   x:Name="CheckItems"   Checked=""/>
                            </DataTemplate>
                        </controls:DataGridTemplateColumn.CellTemplate>
                    </controls:DataGridTemplateColumn>
Developer technologies | Universal Windows Platform (UWP)
SQL Server | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-01-31T08:21:04.183+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You can't directly get the column value from the row directly. There is no API from the DataGrid could do it. But there is another way to implement what you want. You could save the bool flag into the data model that you are using. Then when you need to delete it, go through the model list and check the flag. If the flag is true, then you could remove them.

    So the steps are like this:

    1. Add a new bool property let's say ischecked, to the data model for each row.
    2. Binding the checkbox value to ischecked property of the data model in two way mode. So when the checkbox is checked or unchecked, you could directly get the value from the data model
    3. When you need to delete the value, go through the model list, remove the data based on the ischecked property.

    In this way, there is no more needs to be done in the UI system.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.