【问题标题】:How to add a new row to a datagrid(WPF toolkit) when click on a button in the outside of datagrid单击数据网格外部的按钮时如何向数据网格(WPF工具包)添加新行
【发布时间】:2010-02-27 11:24:34
【问题描述】:

单击数据网格外部的按钮时,我想在数据网格中添加一个新行。 数据网格与 SQL Server 数据库绑定,运行时有一些数据 我想通过数据库向数据库中添加新数据

试了很多次都没成功

谁回复我,对我很有帮助...

提前谢谢..

【问题讨论】:

    标签: c# sql-server wpf .net-3.5


    【解决方案1】:

    如果绑定到网格的数据集合实现了 INotifyCollectionChanged,则向集合中添加新项会将行添加到数据网格中。

    当您从数据库中读取数据时,将其存储在 ObservableCollection(实现此接口)中,然后将数据绑定到网格。

    例子:

    public class ViewModel {
    
       public ObservableCollection<Data> Items { get; set; }
    
       ...
    
    }
    

    在 View.xaml 中:

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

    您必须将视图的 DataContext 属性设置为 ViewModel 的实例。

    从现在开始,在可观察集合中添加/删除项目将自动触发数据网格上的相同操作。

    【讨论】:

      【解决方案2】:

      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>
      

      代码:

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

      【讨论】:

      • 感谢 Bo Persson 编辑我的代码。由于我是堆栈溢出的新手,因此很难理解流程。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 2010-12-13
      • 1970-01-01
      相关资源
      最近更新 更多