نمایش نتایج 1 تا 40 از 1081

نام تاپیک: نمونه برنامه های کوچک و مفيد در سي شارپ

Threaded View

پست قبلی پست قبلی   پست بعدی پست بعدی
  1. #8
    کاربر دائمی آواتار سار
    تاریخ عضویت
    اسفند 1382
    محل زندگی
    تورین-ایتالیا
    پست
    1,044

    Sort كردن Generic List وقتي كه به DataGridView بايند شده

    مشكلي كه مدتي من رو گرفتار خودش كرده بود با كمك لينكي كه linux عزيز داد حل شد.

    مشكل : Sort كردن Generic List وقتي كه به DataGridView بايند شده.
    منبع : ‌http://objectmix.com/csharp/325286-s...ndinglist.html
    جواب :

    publicclass SortableBindingList<T> : BindingList<T>
    {
    protected override bool SupportsSortingCore
    { get { return true; } }
    protected override bool IsSortedCore
    {
    get
    {
    for (int i = 0; i < Items.Count - 1; ++i)
    {
    T lhs = Items[i];
    T rhs = Items[i + 1];
    PropertyDescriptor property = SortPropertyCore;
    if (property != null)
    {
    object lhsValue = lhs == null ? null :
    property.GetValue(lhs);
    object rhsValue = rhs == null ? null :
    property.GetValue(rhs);
    int result;
    if (lhsValue == null)
    {
    result = -1;
    }
    elseif (rhsValue == null)
    {
    result = 1;
    }
    else
    {
    result =
    Comparer.Default.Compare(lhsValue, rhsValue);
    }
    if (SortDirectionCore ==
    ListSortDirection.Descending)
    {
    result = -result;
    }
    if (result >= 0)
    {
    return false;
    }
    }
    }
    return true;
    }
    }
    private ListSortDirection sortDirection;
    protectedoverride ListSortDirection SortDirectionCore
    {
    get
    {
    return sortDirection;
    }
    }
    private PropertyDescriptor sortProperty;
    protectedoverride PropertyDescriptor SortPropertyCore
    {
    get
    {
    return sortProperty;
    }
    }
    protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
    {
    sortProperty = prop;
    sortDirection = direction;
    List<T> list = (List<T>)Items;
    list.Sort(delegate(T lhs, T rhs)
    {
    if (sortProperty != null)
    {
    object lhsValue = lhs == null ? null :
    sortProperty.GetValue(lhs);
    object rhsValue = rhs == null ? null :
    sortProperty.GetValue(rhs);
    int result;
    if (lhsValue == null)
    {
    result = -1;
    }
    elseif (rhsValue == null)
    {
    result = 1;
    }
    else
    {
    result = Comparer.Default.Compare(lhsValue,
    rhsValue);
    }
    if (sortDirection == ListSortDirection.Descending)
    {
    result = -result;
    }
    return result;
    }
    else
    {
    return 0;
    }
    });
    }
    protected override void RemoveSortCore()
    {
    sortDirection = ListSortDirection.Ascending;
    sortProperty = null;
    }
    }
    فایل های ضمیمه فایل های ضمیمه

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •