none
TableAdapterManager order RRS feed

  • Question

  • Hi all,

    if you have a dataset with two tables as master(form1TableAdapter for form1 ) - detail(form2TableAdapter for form2) relation but this relation is used just for delete and update cascading issue where insertion operation uses individual task (By getting foreign key value programatically and use it with insertion fields) but using the same dataset , Now when i drag mentioned dataset.form2TableAdapter on my (Form2) to design my transaction form the tableAdapterManager is automatically created as wellknown for you but not connected to form2TableAdapter so the save code lines have no effects

    this.Validate;

    this.form2TableBindingSource.EndEdit();

    tableAdapterManager .UpdateAll(dataset);

    So what is the best senario in order post my data when i press save button

    Thanks


    Esmat



    • Edited by Esmat_Halfa Tuesday, December 5, 2017 9:44 AM
    Tuesday, December 5, 2017 7:11 AM

All replies

  • Please consider using the Entity Framework. TableAdapters are older technology and not understood by as many other programmers as EF is. Also TableAdapters are nearly all generated code and therefore not documented. TableAdapters are also less flexible.

    I found the following that might help.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, December 5, 2017 8:55 PM
  • Hello,

    I would say to move away from your current path of data wizards and TableAdapter/DataSet/BindingSource and consider either Entity Framework or a managed data provider.

    A fair amount could be written for either direction, Entity Framework or manage data provider.

    Before I used Entity Framework I was well versed in managed data providers which at the core is what EF (and for that matter so do TableAdapters) uses and is good to understand how things work

    If you are fluent with manage data providers than look at EF but know full well with EF there will be more code to write in some cases and other cases much less code. You can do code first, model first or database first. While the "first" methods provide much more flexibility that model first, model first is easier to learn.

    If you go with EF, a stock DataGridView does not know how to order columns via clicking on the column header. You will need the following in tangent with a BindingSource component.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ComponentModel;
    
    /// <summary>
    /// Original source
    /// http://www.martinwilley.com/net/code/forms/sortablebindinglist.html
    /// </summary>
    namespace CustomBindingList_cs
    {
        /// <summary>
        /// Provides a generic collection that supports data binding and additionally supports sorting.
        /// See http://msdn.microsoft.com/en-us/library/ms993236.aspx
        /// If the elements are IComparable it uses that; otherwise compares the ToString()
        /// </summary>
        /// <typeparam name="T">The type of elements in the list.</typeparam>
        public class SortableBindingList<T> : BindingList<T> where T : class
        {
            private bool _isSorted;
            private ListSortDirection _sortDirection = ListSortDirection.Ascending;
            private PropertyDescriptor _sortProperty;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
            /// </summary>
            public SortableBindingList()
            {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
            /// </summary>
            /// <param name="list">An <see cref="T:System.Collections.Generic.IList`1" /> of items to be contained in the <see cref="T:System.ComponentModel.BindingList`1" />.</param>
            public SortableBindingList(IList<T> list)  : base(list) { }
            /// <summary>
            /// Gets a value indicating whether the list supports sorting.
            /// </summary>
            protected override bool SupportsSortingCore
            {
                get { return true; }
            }
            /// <summary>
            /// Gets a value indicating whether the list is sorted.
            /// </summary>
            protected override bool IsSortedCore
            {
                get { return _isSorted; }
            }
            /// <summary>
            /// Gets the direction the list is sorted.
            /// </summary>
            protected override ListSortDirection SortDirectionCore
            {
                get { return _sortDirection; }
            }
            /// <summary>
            /// Gets the property descriptor that is used for sorting the list if sorting is implemented in a derived class; otherwise, returns null
            /// </summary>
            protected override PropertyDescriptor SortPropertyCore
            {
                get { return _sortProperty; }
            }
            /// <summary>
            /// Removes any sort applied with ApplySortCore if sorting is implemented
            /// </summary>
            protected override void RemoveSortCore()
            {
                _sortDirection = ListSortDirection.Ascending;
                _sortProperty = null;
                _isSorted = false; //thanks Luca
            }
            /// <summary>
            /// Sorts the items if overridden in a derived class
            /// </summary>
            /// <param name="prop"></param>
            /// <param name="direction"></param>
            protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
            {
                _sortProperty = prop;
                _sortDirection = direction;
    
                List<T> list = Items as List<T>;
                if (list == null) return;
    
                list.Sort(Compare);
    
                _isSorted = true;
                //fire an event that the list has been changed.
                OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
            }
    
    
            private int Compare(T lhs, T rhs)
            {
                var result = OnComparison(lhs, rhs);
                //invert if descending
                if (_sortDirection == ListSortDirection.Descending)
                    result = -result;
                return result;
            }
    
            private int OnComparison(T lhs, T rhs)
            {
                object lhsValue = lhs == null ? null : _sortProperty.GetValue(lhs);
                object rhsValue = rhs == null ? null : _sortProperty.GetValue(rhs);
                if (lhsValue == null)
                {
                    return (rhsValue == null) ? 0 : -1; //nulls are equal
                }
                if (rhsValue == null)
                {
                    return 1; //first has value, second doesn't
                }
                if (lhsValue is IComparable)
                {
                    return ((IComparable)lhsValue).CompareTo(rhsValue);
                }
                if (lhsValue.Equals(rhsValue))
                {
                    return 0; //both are the same
                }
                //not comparable, compare ToString
                return lhsValue.ToString().CompareTo(rhsValue.ToString());
            }
        }
    }
    

    See it being used in my MSDN code sample (not master details but that is not relevant here. Going with master details, the following goes master/details-master/details (two masters, two details where the second details is also the second master).

    https://code.msdn.microsoft.com/Windows-forms-detail-view-89372f21


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, December 6, 2017 12:56 AM
    Moderator