0

I have listview to which I add the names of cities. I have a field to enter ID and a button to delete the name. Is there an option to place an X button on each line against each added name to delete the name without manually adding ID and pressing a button ?

My xaml code:

            <StackLayout HorizontalOptions="Center" VerticalOptions="Start">
            <Label Margin="0,0,0,10" Text="SQLite" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
            <Entry x:Name="txtPersonId" Placeholder="PersonId Update and Delete"></Entry>
            <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
            <Button x:Name="btnDelete" WidthRequest="200" Text="Delete" Clicked="BtnDelete_Clicked" />
            </StackLayout>
            <ListView x:Name="lstPersons">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Name}" Detail="{Binding PersonID}"></TextCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            </StackLayout>

My xaml.cs look like:

 private async void BtnDelete_Clicked(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtPersonId.Text))
        {
            //Get Person
            var person = await App.SQLiteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
            if (person != null)
            {
                //Delete Person
                await App.SQLiteDb.DeleteItemAsync(person);
                txtPersonId.Text = string.Empty;
                await DisplayAlert("Success", "Person Deleted", "OK");

                //Get All Persons
                var personList = await App.SQLiteDb.GetItemsAsync();
                if (personList != null)
                {
                    lstPersons.ItemsSource = personList;
                }
            }
        }
        else
        {
            await DisplayAlert("Required", "Please Enter PersonID", "OK");
        }
    }
1
  • use a ViewCell instead of a TextCell and add whatever UI you need in the ViewCell Commented May 15, 2021 at 11:23

1 Answer 1

1

How to set x button for delete records on database on every row using listview-xamarin

You can take a look custom cell in ListView firstly, then take a look the following code:

   <ListView x:Name="lstPersons">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding PersonID}" />
                            <Label HorizontalOptions="CenterAndExpand" Text="{Binding Name}" />
                            <Button
                                Clicked="Button_Clicked"
                                HorizontalOptions="EndAndExpand"
                                Text="Delete a record" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Then you can delete current ListView row by Button.click, loading ListView data again.

  private void Button_Clicked(object sender, EventArgs e)
    {
        var btn = sender as Button;
        var person = btn.BindingContext as personclass;
        //delete this item.
        //......
    }
Sign up to request clarification or add additional context in comments.

Comments

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.