Lukman
Lukman

Reputation: 101

How to add a new row to a datagrid(WPF toolkit) when click on a button in the outside of datagrid

I want to add a new row in the datagrid when click on a button in the outside of a datagrid. The datagrid is bound with SQL Server database, and it have some data at runtime I want to add new data to the database through the database

I tried a lot, but it was unsuccessful

Anyone reply me it will be very helpful for me...

Advance thanks..

Upvotes: 0

Views: 2511

Answers (2)

Archit Arora
Archit Arora

Reputation: 21

XAML:

<Window x:Class="NewItemEvent.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="341" Width="567"   xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
<Grid>
<my:DataGrid AutoGenerateColumns="False" Margin="0,0,0,29" Name="dataGrid1">
  <my:DataGrid.Columns>
    <my:DataGridTemplateColumn Header="Name" Width="150">
      <my:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}" Margin="3 3 3 3"/>
            <TextBlock Text="{Binding LastName}" Margin="3 3 3 3"/>
          </StackPanel>
        </DataTemplate>
      </my:DataGridTemplateColumn.CellTemplate>
      <my:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding FirstName}" Margin="3 3 3 3"/>
            <TextBox Text="{Binding LastName}" Margin="3 3 3 3"/>
          </StackPanel>
        </DataTemplate>
      </my:DataGridTemplateColumn.CellEditingTemplate>
    </my:DataGridTemplateColumn>
    <my:DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="100"/>
  </my:DataGrid.Columns>
</my:DataGrid>
<Button Height="23" HorizontalAlignment="Left" Name="AddNewRow" Click="AddNewRow_Click" VerticalAlignment="Bottom" Width="75">New Row</Button>

Code:

/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
   ObservableCollection<Person> People = new ObservableCollection<Person>();
   public Window1()
   {
       InitializeComponent();
       dataGrid1.ItemsSource = People;
   }

   private void AddNewRow_Click(object sender, RoutedEventArgs e)
   {
      People.Add(new Person() { FirstName = "Tom", LastName = "Smith", Age = 20 });
   }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

Upvotes: 0

Timores
Timores

Reputation: 14589

If the collection of data bound to the grid implements INotifyCollectionChanged, adding a new item to the collection will add the row to the datagrid.

When you read the data from the DB, store it in an ObservableCollection (which implements this interface), then bind the data to the grid.

Example:

public class ViewModel {

   public ObservableCollection<Data> Items { get; set; }

   ...

}

In View.xaml:

...
<DataGrid ItemsSource={Binding Path=Items}" ... />
...

And you have to set the DataContext property of the view to an instance of ViewModel.

From now on, adding/removing items from the observable collection will automatically trigger the same operation on the data grid.

Upvotes: 2

Related Questions