c# - Deleting rows in a datagrid -


I have recently moved from WinForms to WPF and it seems that it is more complicated to deal with datagrids. . . Data grid x; & lt :: name = "When I run the application manually in the datagrid from the user interface to the datagrid

I have done the XAML code following the DataGridAdd" HorizontalAlignment = "Left" margin = "591,360,0,0" VerticalAlignment = "top" height = "118" width = "202" AutoGenerateColumns = "false" ScrollViewer.CanContentScroll = "false" VerticalScrollBarVisibility = "Disabled" grid. Column = "1" canuser diameter = "true" & gt; & Lt; DataGrid.Columns & gt; & Lt; DataGridTextColumn = Binding "{Binding Varenr}" ClipboardContentBinding = "{x: Nulled}" Header = "Vare Nr .:" width = "100" /> & Lt; DataGridTextColumn binding = "{binding antal}" clipboard contentbinding = "{x: ft}" header = "antal" width = "100" /> & Lt; /DataGrid.Columns> & Lt; / Data grid & gt;

and background code:

  public partial squares window 1: window {observable collection & lt; Add & gt; List; Public Window 1 () {InitializeComponent (); List = New Observational Collection & lt; Add & gt; (); DataGridAdd.ItemsSource = List; } Add public category {public string Varenr {get; Set; } Receive public string {receive; Set; }} Add Private Zero Button Print Clip (Object Sender, RoutedEventArgs e) addval = DataGridAdd.SelectedItem Add; While (DatagreatsAd.Entimes.Count & gt; 1) {string vare = addval.Varenr; Wire antal1 = addval.Antal; List.Remove (addval); }}}   

The issue is that, when I am adding data to a row and pressing the print button, it removes the expected objects: < P> Enter image details here

The problem occurs when I use several rows Adding:

Enter image details here

while The loop gets infinite, because the DataGridAd.Eight. In this case the value is not less than "2". How can I handle this problem? For example, if you have more than 2 items in the DataGrid Ad, for example, if you have more than 2 items in the Data Grid Ad, So your code will be excited like this:

  1. added addval = as DataGridAdd.SelectedItem;
  2. While (dataGridAdd.Items.Count> 3) // dataGridAdd.Items.Count = 3, list.Count = 3
  3. string vare = addval.Varenr; Wire antal1 = addval.Antal;
  4. List. Remove (add); // True, delete the return successfully, list.count = 2
  5. While (dataGridAdd.Items.Count> 2) / / False, DataGrid add.Items.Count = 2 < Li> string Vare = addval.Varenr; String antal1 = addval.Antal;
  6. List. Remove (additional); // Return false, failed to delete, list.count = 2 ....

    Then, the DataGrid Ad.its.Count Value does not go below "2"

    By the way, why use this?


    Update:

    To run this code, and you dataGridAdd.SelectedIndex = -1 and < After the code> Data Grid Ad.itedEdit = empty list. Remove (adwale); So, if you remove again, this flat will return.

      Private Zero Button_Click (Object Sender, RoutedEventArgs e) [Addval = dataGridAdd.SelectedItem Add as add; Console.WriteLine (dataGridAdd.SelectedIndex); Console.WriteLine (dataGridAdd.SelectedItem); String vare = addval.Varenr; Wire antal1 = addval.Antal; List.Remove (addval); Console.WriteLine (dataGridAdd.SelectedIndex); Console.WriteLine (dataGridAdd.SelectedItem); }    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -