2012-05-30 3 views
0

데이터 소스의 데이터 소스 역할을하는 바인딩 소스가 있습니다. 바인딩 소스를 바인딩 한 후 바인딩 소스의 두 행을 교체하려고합니다. 나는 아이디어가 결합 소스바인딩 소스에서 두 항목을 바꿉니다.

OnlineOutageReport[] outageReports; 
dashBoardBindingSource = new BindingSource(); 
      dashBoardBindingSource.DataSource = ToDataTable<OnlineOutageReport>(outageReports); 


private static DataTable ToDataTable<T>(IList<T> data) 
    { 
     PropertyDescriptorCollection props = 
      TypeDescriptor.GetProperties(typeof(T)); 
     DataTable table = new DataTable(); 
     for (int i = 0; i < props.Count; i++) 
     { 
      PropertyDescriptor prop = props[i]; 
      table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
     prop.PropertyType) ?? prop.PropertyType); 

     } 
     object[] values = new object[props.Count]; 
     foreach (T item in data) 
     { 
      for (int i = 0; i < values.Length; i++) 
      { 
       values[i] = props[i].GetValue(item); 
      } 
      table.Rows.Add(values); 
     } 
     return table; 
    } 

누구에 데이터를 바인딩하는 다음 코드를 사용하고

Cannot set an object into this list. 

을 다음과 같이

object currentRow = dashBoardBindingSource[index]; 
        object aboveRow = dashBoardBindingSource[index - 1]; 
        dashBoardBindingSource[index] = aboveRow; 
        dashBoardBindingSource[index - 1] = currentRow; 

위의 코드는 나에게 예외를 제공합니다. 감사합니다.

+0

dashBoardBindingSource.DataSource = outageReports; 잘 작동합니다.하지만 dashBoardBindingSource.Sort = sortOrder; 그것은 작동하지 않습니다. 내가 잘하면 – udaya726

+0

dashBoardBindingSource.DataSource = outageReports; 그것의 hows.But 내 기준에 따라 바인딩 소스를 정렬 싶어요. 그게 내가 왜 그것을 데이터 테이블로 변환하는지. 변환 후 그것을 정렬 잘 작동합니다. – udaya726

답변

1

DataGridView의 한 행에서 다른 행으로 bindingSource를 이동하는 방법은 다음과 같습니다.

  int index = this.KeyDataGridView.SelectedRows[0].Index; 

      this.KeyDataGridView.ClearSelection(); 
      if (index == this.KeyDataGridView.Rows.Count - 1) 
      { 
       return;//can go down 
      } 
      var tempKey = this.keysBindingSource[index]; 
      this.keysBindingSource.RemoveAt(index); 
      this.keysBindingSource.Insert(index + 1, tempKey); 
그것은 당신이 dataSource.Insert (인덱스)를 사용하는 데 필요한 단지 수

대신

관련 문제