Add new Row to DataTable

前端 未结 4 811
囚心锁ツ
囚心锁ツ 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: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

    
    

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

提交回复
热议问题