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..

Answers


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.


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; }
}

Need Your Help

Order By character varying time strings?

sql postgresql postgresql-9.1

I'm querying a table and I'm trying to Order By a "time" column that has the time formatted as "1 - 2 p.m.", "7 - 8 a.m.", "11 a.m. - 12 p.m.", etc etc. I'm not allowed to alter this column, but I...

Fibonacci Sequence error

eclipse fibonacci

I am coding a Fibonacci sequence in Eclipse and this is my code-

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.