locked
Datagrid Delete/Update Selected Row RRS feed

  • Question

  • Hello. I work in WPF.

    I have a datagrid and I want to delete the selected row by pressing button x and I want to update some values of the selected row by pressing the button Y.  Is this possible? I searched a lot in the internet but I haven't found a solution..

    Thank you

    Christos

    Tuesday, September 23, 2014 1:39 AM

Answers

  • Hello. Thank you for your answer.  Unfortunately I am adding data programmatically (sorry I didn't mention), like this:

    var data = new BindingData { No = "1", Description = "test", Price = "test", Quantity = "1", Amount = "1", Status = "Default" };
    datagrid.Items.Add(data);

    So in this case what do we do ? How can I delete/update ??

    Thanks


    Hi Christose,

    >>So in this case what do we do ? How can I delete/update ??

    Similar to my previous reply, here is my latest demo based on your scenario:

    XAML:

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <DataGrid Name="datagrid1" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="No" Binding="{Binding No}" />
                    <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
                    <DataGridTextColumn Header="Price" Binding="{Binding Price}" />
                    <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
                    <DataGridTextColumn Header="Amount" Binding="{Binding Amount}" />
                    <DataGridTextColumn Header="Status" Binding="{Binding Status}" />
                </DataGrid.Columns>
            </DataGrid>
            <Button Grid.Row="1" Name="BtnDel" Width="100" Content="Delete Row" HorizontalAlignment="Left" Click="BtnDel_Click" />
            <Button Grid.Row="1" Name="BtnUpdate" Width="100" Content="Update Row" HorizontalAlignment="Right" Click="BtnUpdate_Click" />
        </Grid>


    Code Behind:

    public MainWindow()
    {
                InitializeComponent();
    
                datagrid1.Items.Add(new BindingData { No = "1", Description = "test", Price = "test", Quantity = "1", Amount = "1", Status = "Default" });
                datagrid1.Items.Add(new BindingData { No = "2", Description = "test", Price = "test", Quantity = "2", Amount = "2", Status = "Default" });
                datagrid1.Items.Add(new BindingData { No = "3", Description = "test", Price = "test", Quantity = "3", Amount = "3", Status = "Default" });
    }
    
    private void BtnDel_Click(object sender, RoutedEventArgs e)
    {
                if (datagrid1.SelectedItem != null)
                {
                    datagrid1.Items.Remove((BindingData)datagrid1.SelectedItem);
                }
    }
    
    private void BtnUpdate_Click(object sender, RoutedEventArgs e)
    {
                if (datagrid1.SelectedItem != null)
                {
                    BindingData item = (BindingData)datagrid1.SelectedItem;
                    item.Description = "Changed";
                    item.Price = "Changed";
    
                    datagrid1.Items.Refresh();
                }
    }

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Magnus (MM8)MVP Wednesday, September 24, 2014 10:43 AM
    • Marked as answer by Christose88 Wednesday, October 1, 2014 1:47 AM
    Wednesday, September 24, 2014 2:45 AM

All replies

  • Hi,

    >>I want to delete the selected row by pressing button x and I want to update some values of the selected row by pressing the button Y

    Yes, it is possible. We can use SelectedItem property to get the data item corresponding to the selected row:

    #DataGrid.SelectedItem Property
    http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.selecteditem(v=vs.95).aspx

    Then we can remove item from the ItemsSource or modify data as you like, here is my simple sample:

    XAML:

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <DataGrid Name="datagrid1" ItemsSource="{Binding}" />
            <Button Grid.Row="1" Name="BtnDel" Width="100" Content="Delete Row" HorizontalAlignment="Left" Click="BtnDel_Click" />
            <Button Grid.Row="1" Name="BtnUpdate" Width="100" Content="Update Row" HorizontalAlignment="Right" Click="BtnUpdate_Click" />
        </Grid>

    public partial class MainWindow : Window
    {
            ObservableCollection<TestClass> oc = new ObservableCollection<TestClass>();
            public MainWindow()
            {
                InitializeComponent();
    
                oc.Add(new TestClass() { Column1="Col1-1", Column2="Col1-2", Column3="Col1-3" });
                oc.Add(new TestClass() { Column1 = "Col2-1", Column2 = "Col2-2", Column3 = "Col2-3" });
                oc.Add(new TestClass() { Column1 = "Col3-1", Column2 = "Col3-2", Column3 = "Col3-3" });
                oc.Add(new TestClass() { Column1 = "Col4-1", Column2 = "Col4-2", Column3 = "Col4-3" });
    
                this.DataContext = oc;
            }
    
            private void BtnDel_Click(object sender, RoutedEventArgs e)
            {
                if (datagrid1.SelectedItem != null)
                {
                    oc.Remove((TestClass)datagrid1.SelectedItem);
                }
            }
    
            private void BtnUpdate_Click(object sender, RoutedEventArgs e)
            {
                if (datagrid1.SelectedItem != null)
                {
                    TestClass item = (TestClass)datagrid1.SelectedItem;
                    item.Column1 = "1 Changed";
                    item.Column2 = "2 Changed";
                    item.Column3 = "3 Changed";
    
                    datagrid1.Items.Refresh();
                }
            }
    }
    
    public class TestClass
    {
            public string Column1 { get; set; }
            public string Column2 { get; set; }
            public string Column3 { get; set; }
    }

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 24, 2014 2:14 AM
  • Hello. Thank you for your answer.  Unfortunately I am adding data programmatically (sorry I didn't mention), like this:

    var data = new BindingData { No = "1", Description = "test", Price = "test", Quantity = "1", Amount = "1", Status = "Default" };
    datagrid.Items.Add(data);

    So in this case what do we do ? How can I delete/update ??

    Thanks

    Wednesday, September 24, 2014 2:28 AM
  • Hello. Thank you for your answer.  Unfortunately I am adding data programmatically (sorry I didn't mention), like this:

    var data = new BindingData { No = "1", Description = "test", Price = "test", Quantity = "1", Amount = "1", Status = "Default" };
    datagrid.Items.Add(data);

    So in this case what do we do ? How can I delete/update ??

    Thanks


    Hi Christose,

    >>So in this case what do we do ? How can I delete/update ??

    Similar to my previous reply, here is my latest demo based on your scenario:

    XAML:

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <DataGrid Name="datagrid1" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="No" Binding="{Binding No}" />
                    <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
                    <DataGridTextColumn Header="Price" Binding="{Binding Price}" />
                    <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
                    <DataGridTextColumn Header="Amount" Binding="{Binding Amount}" />
                    <DataGridTextColumn Header="Status" Binding="{Binding Status}" />
                </DataGrid.Columns>
            </DataGrid>
            <Button Grid.Row="1" Name="BtnDel" Width="100" Content="Delete Row" HorizontalAlignment="Left" Click="BtnDel_Click" />
            <Button Grid.Row="1" Name="BtnUpdate" Width="100" Content="Update Row" HorizontalAlignment="Right" Click="BtnUpdate_Click" />
        </Grid>


    Code Behind:

    public MainWindow()
    {
                InitializeComponent();
    
                datagrid1.Items.Add(new BindingData { No = "1", Description = "test", Price = "test", Quantity = "1", Amount = "1", Status = "Default" });
                datagrid1.Items.Add(new BindingData { No = "2", Description = "test", Price = "test", Quantity = "2", Amount = "2", Status = "Default" });
                datagrid1.Items.Add(new BindingData { No = "3", Description = "test", Price = "test", Quantity = "3", Amount = "3", Status = "Default" });
    }
    
    private void BtnDel_Click(object sender, RoutedEventArgs e)
    {
                if (datagrid1.SelectedItem != null)
                {
                    datagrid1.Items.Remove((BindingData)datagrid1.SelectedItem);
                }
    }
    
    private void BtnUpdate_Click(object sender, RoutedEventArgs e)
    {
                if (datagrid1.SelectedItem != null)
                {
                    BindingData item = (BindingData)datagrid1.SelectedItem;
                    item.Description = "Changed";
                    item.Price = "Changed";
    
                    datagrid1.Items.Refresh();
                }
    }

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Magnus (MM8)MVP Wednesday, September 24, 2014 10:43 AM
    • Marked as answer by Christose88 Wednesday, October 1, 2014 1:47 AM
    Wednesday, September 24, 2014 2:45 AM
  • Hello Franklin. 

    I saw your immediate response but I was away.  It works perfectly, you are a genius! :)

    Also I found a solution for fixing the numbering of the items in the table after the delete! :)

    Thank you for your time and big thanks for your fast response on my question.

    Christos

    Wednesday, October 1, 2014 1:52 AM
  • Hello Franklin. 

    I saw your immediate response but I was away.  It works perfectly, you are a genius! :)

    Also I found a solution for fixing the numbering of the items in the table after the delete! :)

    Thank you for your time and big thanks for your fast response on my question.

    Christos


    You're welcome, Christos:)

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 1, 2014 2:08 AM