2017-10-27 6 views
0
이라는 Schneider 제품을 사용하여 .Net 인터페이스를 통해 독점적 인 백엔드 데이터베이스를 쿼리 할 수 ​​있습니다. 나는 성공적으로이 쿼리를 사용하여 DB를 조회 한 :

ReadOnlyCollection을 DataGridView에 바인딩하는 방법

SELECT 
     "Id", "FullName", "Foreground", "Blink", "Background", "TypeDesc", "MemoryUsage" 
    FROM 
     "CCustomColour" 
    ORDER BY 
     "FullName" ASC 

및 결과는 QueryResult으로 나에게 돌아왔다 : 나는 데이터 그리드 다른 몇 가지에 행의 ReadOnlyCollection을 결합하는 시도

using System.Collections.ObjectModel; 

    namespace ClearScada.Client.Advanced 
    { 
     // 
     public class QueryResult 
     { 
      // 
      // Summary: 
      //  Gets the results of the query. 
      public ReadOnlyCollection<QueryRow> Rows { get; } 
      // 
      // Summary: 
      //  Gets the number of rows affected for a DML query. 
      public int RowsAffected { get; } 
      // 
      // Summary: 
      //  Gets the status of the query. 
      public QueryStatus Status { get; } 
     } 
    } 

방법.

dataGridViewResults.DataSource = this.MyQueryResult.Rows; 

   BindingSource bs = new BindingSource(); 
       bs.DataSource = this.MyQueryResult.Rows; 
       dataGridViewResults.DataSource = bs; 

격자 날만을 행 인덱스하지만 데이터를 나타내는 단일 열을 도시한다 : 여기에 참고로

enter image description here

는 QueryRow 클래스의 정의이며 :

using System.Collections.ObjectModel; 

namespace ClearScada.Client.Advanced 
{ 
    // 
    public class QueryRow 
    { 
     // 
     public ReadOnlyCollection<object> Data { get; } 
     // 
     public int Index { get; } 
    } 
} 

View of the QueryResult in the 'Locals' window of Visual Studio

이 무엇을 달성하려고하는 것은 : 여기 보여 비주얼 스튜디오에서 '현지인'창에서의 전망을 캡처 할 수 있도록

0 내 QueryResult 인스턴스의 로컬 복사본을 생성

enter image description here

당신은 내가있는 DataGridView에 그 값을 바인딩 할 수있는 방법을 나에게 보여줌으로써 좀 도와 주시겠습니까 : 다음과 같이 표시하는보기?

답변

0

내가 더 나은 방법이있을 확신하지만이 DataGridView에 해당 바인딩 다음의 DataTable에 데이터를 전송에 의해 나를 위해 노력하고 있습니다 : 누군가가 설명 할 수있는 경우

   if (this.MyQueryStatus.Equals(QueryStatus.Succeeded)) 
       { 
        #region IF Succeeded 
        #region Create Results DataTable 
        DataTable resultsTable = new DataTable("Results"); 
        resultsTable.Columns.Add("Id"); 
        resultsTable.Columns.Add("FullName"); 
        resultsTable.Columns.Add("Foreground"); 
        resultsTable.Columns.Add("Blink"); 
        resultsTable.Columns.Add("Background"); 
        resultsTable.Columns.Add("TypeDesc"); 
        resultsTable.Columns.Add("MemoryUsage"); 
        #endregion Create Results DataTable 

        foreach (var row in this.MyQueryResult.Rows) 
        { 
         #region ForEach Row 
         DataRow newRow = resultsTable.NewRow(); 
         newRow["Id"] = row.Data[0].ToString(); 
         newRow["FullName"] = row.Data[1].ToString(); 
         newRow["Foreground"] = row.Data[2].ToString(); 
         newRow["Blink"] = row.Data[3].ToString(); 
         newRow["Background"] = row.Data[4].ToString(); 
         newRow["TypeDesc"] = row.Data[5].ToString(); 
         newRow["MemoryUsage"] = row.Data[6].ToString(); 
         resultsTable.Rows.Add(newRow); 
         #endregion ForEach Row 
        } 

        dataGridViewResults.DataSource = resultsTable; 
        tabControlMain.SelectedTab = tabControlMain.TabPages["tabPageResults"]; 
        #endregion IF Succeeded 
       } 

난 아직도 감사하겠습니다 ReadOnlyCollection을 DataGridView에 직접 바인딩하는 방법

+0

'dataGridViewResults.DataSource = this.MyQueryResult.Rows.ToList()'를 시도 해 보셨습니까? – Ingenioushax

+0

아이디어를 주셔서 감사 드리며, 저는 이것을 구현하려고 시도했지만 'this.MyQueryResult.Rows'에는 ToList 메소드가 없습니다. 그것은 단지 두 개의 메소드, CopyTo와 ToString만을 노출합니다. CopyTo 메서드를 사용하면 행의 ReadOnlyCollection을 QueryRow 개체의 배열에 복사 할 수 있습니다. 나는 이것을 시도한 다음 결과 배열에서 ToList 메서드를 찾았지만 다시 ToList 메서드가 없습니다. – Experimentalist

+0

직선 변환 대신 목록 생성자는 어떻게됩니까? 'List mqr = 새로운 목록 (this.MyQueryResults);'? – Ingenioushax