Add new Row to DataTable

前端 未结 4 813
囚心锁ツ
囚心锁ツ 2021-01-16 03:46

I have a DataGrid bind to a database with one Table and one Column (FooTable and FooName). With my following code, I can bind DataGrid to Dat

相关标签:
4条回答
  • 2021-01-16 04:24

    Try calling dt.DataBind(); at the end.

    0 讨论(0)
  • 2021-01-16 04:30

    Hi i edit my answer in your other thread .

    the general way i use when working with datatables, datagrid and wpf/mvvm:

    i always bind the datagrid itemssource to the BindingListCollectionView of the datatable. i do this because i can easily filter, refresh or add/delete/modify the datatable items.

    0 讨论(0)
  • 2021-01-16 04:39

    pls post your xaml or cs code where you bind the Itemssource.

    does your DataSet_Add_Click add the row to your datatable? if yes then it seems is just a refresh/binding problem with your datagrid.

    when i work with datatables and datagrid i always use the following

    //ctor or init
    dt = ds.Tables["FooTable"];
    //the next line you have to write after you initialize your datatable
    this.MyView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.dt);
    

    XAML

    <DataGrid ItemsSource="{Binding MyView }"/>
    

    Refresh

    this.MyView.Refresh();
    

    EDIT: MyView is a property

    public BindingListCollectionView MyView
    { 
       get {return this._myview;}
       set {this._myview = value; OnPropertyChanged("MyView");
    }
    

    you can do binding in code.

     Binding myBinding = new Binding("MyView");
     myBinding.Source = this;//the instance with the MyView property
     mydatagridctrl.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
    
    0 讨论(0)
  • 2021-01-16 04:44

    Try adding this to the end of your DataSet_Add_Click method

    dg1.Items.Refresh();
    
    0 讨论(0)
提交回复
热议问题