본문 바로가기
.NET

Telerik RadGrid Refresh, 동적으로 Binding data 변경 후 GridView Refresh

by leo21c 2022. 6. 3.
SMALL

Telerik의 RadGridView를 사용하고 있는데 DataSource로 바인딩한 정보를 동적으로 변경하면 화면에 바로 업데이트 되지 않는다.

 

가장 기본적인 방법은 DataSource = null로 초기화를 하고 다시 바인딩을 하는 것이다.

이렇게 하면 당연히 동적으로 변경된 데이터가 화면에 보일 것이다. 하지만 이렇게 하면 화면이 깜박거리거나 데이터가 많으면 표시되는 속도가 느린 문제가 있다.

 

WPF나 Winform 모두 처리하는 방법은 같다.

검색을 해보니 아래와 같은 정보를 찾을 수 있었다.

 

https://docs.telerik.com/devtools/winforms/controls/gridview/populating-with-data/reflecting-custom-object-changes-in-rgv?_ga=2.209369682.1731047227.1654215181-930908756.1653617507 

 

Reflecting Custom Object Changes in RGV | RadGridView | Telerik UI for WinForms

RadGridView is capable of fetching bindable properties and data. However, one important issue must be noted: during the data binding process, the grid extracts the data from the data source, but for any later changes in the data source, RadGridView should

docs.telerik.com

  • The collection that you will bind to RadGridView should implement IBindingList or IBindingListView interfaces. This will allow RadGridView to get notified about insert and delete operations of records.
  • Your business objects should implement INotifyPropertyChanged interface (.NET 2.0). This will allow RadGridView to reflect changes which occur to the properties of the business objects.

방법은 아주 간단했다.

Binding 하는 클래스의  INotifyPropertyChanged interface를 추가하는 것이다.

 

예제 소스를 보면 아래와 같다.

public class Student : System.ComponentModel.INotifyPropertyChanged
{
    int m_id;
    string m_name;
    string m_grade;
    public event PropertyChangedEventHandler PropertyChanged;
    public Student(int m_id, string m_name, string m_grade)
    {
        this.m_id = m_id;
        this.m_name = m_name;
        this.m_grade = m_grade;
    }
    public int Id
    {
        get
        {
            return m_id;
        }
        set
        {
            if (this.m_id != value)
            {
                this.m_id = value;
                OnPropertyChanged("Id");
            }
        }
    }
    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            if (this.m_name != value)
            {
                this.m_name = value;
                OnPropertyChanged("Name");
            }
        }
    }
    public string Grade
    {
        get
        {
            return m_grade;
        }
        set
        {
            if (this.m_grade != value)
            {
                this.m_grade = value;
                OnPropertyChanged("Grade");
            }
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

위와 같은 방식으로 Binding 하는 Class의 public 변수를 Set 할 때 OnPropertyChanged 처리를 하면 이벤트가 발생하고 GridView의 Column이 업데이트가 된다.

LIST