Good afternoon, tell me how to update the label by clicking on the button:

For example, there is a table in which values ​​are displayed from the database, I hang the handler on the button, which should remove one record from this table in this way:

MySqlCommand query = new MySqlCommand("SQL ZAPROS", connection); connection.Open(); MySqlDataReader query_result = query.ExecuteReader(); connection.Close(); 

What can I add to the content dynamically updated after the execution of this code? That is, the record immediately disappeared.

    1 answer 1

    I decided myself, I just had to update the collection to which the output from the database was attached.

    Solution: The class in which all this happens implements the interface INotifyPropertyChanged Element in XAML markup:

     <ListView x:Name="contains" ItemsSource="{Binding ItemsForContains}" SelectedItem="{Binding SelectedItemsContains}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding UserLogin}"/> <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <Image x:Name="clear" Style="{StaticResource image}" MouseLeftButtonUp="clearProf_MouseLeftButtonUp" ToolTip="ΠžΡ‡ΠΈΡΡ‚ΠΈΡ‚ΡŒ" Source="images/clear.png"></Image> // Кнопка </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> 

    Properties:

     public ObservableCollection<Contains> ItemsForContains { get; set; } private Contains _SelectedItemsContains; public Contains SelectedItemsContains { get { return _SelectedItemsContains; } set { _SelectedItemsContains = value; RaisePropertyChanged(); } } 

    Implementing INotifyPropertyChanged:

     protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); public event PropertyChangedEventHandler PropertyChanged; 

    The output from the database was as follows:

     ItemsForContains = new ObservableCollection<Contains>(); connection.Open(); MySqlCommand contains = new MySqlCommand("SELECT * FROM groups WHERE groupId='" + id + "' LIMIT " + num, connection); // Π²Ρ‹Π±ΠΎΡ€ΠΊΠ° ΠΈΠ· Π‘Π” MySQL MySqlDataReader contains_result = contains.ExecuteReader(); if (contains_result.HasRows) { while (contains_result.Read()) // заполняСм ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ Π² Ρ†ΠΈΠΊΠ»Π΅ { ItemsForContains.Add( new Contains() { CId = contains_result["id"].ToString(), UserId = contains_result["userId"].ToString(), UserLogin = contains_result["userLogin"].ToString() }); } connection.Close(); } 

    Action with base:

     private void clear_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) // ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΊΠ»ΠΈΠΊΠ° ΠΏΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ΅, ΠΊΠ½ΠΎΠΏΠΊΠ° находится Π²Π½ΡƒΡ‚Ρ€ΠΈ SelectedItemsContains { if (SelectedItemsContains != null) // SelectedItemsContains - Π²Ρ‹Π±Ρ€Π°Π½Π½Ρ‹ΠΉ элСмСнт ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ (привязан ΠΊ SelectedIndex). { string connectionString = "SERVER = localhost; DATABASE = root; UID = vbnz; PASSWORD = ;"; MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand query = new MySqlCommand("UPDATE groups SET userLogin = '-' WHERE id = '" + SelectedItemsContains.CId + "' LIMIT 1", connection); connection.Open(); MySqlDataReader query_result = query.ExecuteReader(); connection.Close(); Cursor = Cursors.Wait; Thread.Sleep(300); Cursor = Cursors.Arrow; SelectedItemsContains.UserLogin = "-"; // РСшСниС } } 

    I do not know how correct it is, but it solved the problem.

    • Write a solution to help someone. - Vadim Prokopchuk