Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
110
Adding a column with Delete Row link or button
posted

How can I programmatically do what pressing Delete does when an element in a column of a row is clicked?  It's not intuitive to our customer that you select the row and press the Delete key, so they want a cell in each row which they click to delete the row.  We want it to do the same thing as pressing Delete (same prompting, same removal of record).

Parents
  • 6867
    posted

    There are a few ways to set this up.  Here's one way, which involves removing the data item from the data source. Note, for this to work, the data source must implement INotifyCollectionChanged so that the grid is able to detect the item removal.

     <igDP:UnboundField Name="Delete">
      <igDP:Field.Settings>
        <igDP:FieldSettings>
          <igDP:FieldSettings.CellValuePresenterStyle>
            <Style TargetType="{x:Type igDP:CellValuePresenter}">
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                    <Button Click="OnDeleteButtonClick" HorizontalAlignment="Center">X</Button>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </igDP:FieldSettings.CellValuePresenterStyle>
        </igDP:FieldSettings>
      </igDP:Field.Settings>
    </igDP:UnboundField>

    In the code-behind...

    private void OnDeleteButtonClick(object sender, RoutedEventArgs e)
    {
        Button b = sender as Button;
        if (b == null)
            return;

        DataRecord dr = b.DataContext as DataRecord;
        if (dr == null)
            return;

        ObservableCollection<Task> dataSource = this.xamDG.DataSource as ObservableCollection<Task>;
        if (dataSource == null)
            return;

        dataSource.Remove(dr.DataItem as Task);
    }

Reply Children