2010-07-01 4 views
0

Windows Form 응용 프로그램에서 두 개의 열로 DataGridView를 만들려고합니다. 하나는 XML 요소에서 제공된 키와 하나는 XML 요소의 값입니다. . 그 부분이 작동되도록 내가 data의 모든 정보가 foreach 문을 통해로드되고 있음을 확인했다C에서 키 - 값 쌍으로 DataGridView를 만들려고 시도

 this.myData = new DataGridView(); 
     ((System.ComponentModel.ISupportInitialize)(myData)).BeginInit(); 

     myData.Location = new System.Drawing.Point(12, 42); 
     myData.Name = "myData"; 
     myData.Size = new System.Drawing.Size(1060, 585); 
     myData.TabIndex = 32; 

     foreach (XElement xElem in xInfoItems) 
     { 
      numItems++; 
     } 

     myData.Columns.Add(new DataGridViewTextBoxColumn()); 
     myData.Columns.Add(new DataGridViewTextBoxColumn()); 
     myData.Columns[0].Name = "Key"; 
     myData.Columns[0].DataPropertyName = "key"; 
     myData.Columns[1].Name = "Value"; 
     myData.Columns[1].DataPropertyName = "value"; 

     List<myRow> data = new List<myRow>(); 


     foreach (XElement xElem in xInfoItems) 
     { 
      data.Add(new myRow(xElem.Attribute("key").Value, xElem.Value)); 
     } 

     myData.DataSource = data; 

     myData.Refresh(); 

     this.PerformLayout(); 

이 지금까지 내 코드입니다. 내 문제는 그리드가 표시되지만 아무 것도 그리드에 나타나지 않는다는 것입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 이 데이터 유형이 좋지 않아서 사과해야합니다.

UPDATE

은 내가 디자인 뷰에서 제대로 MYDATA을 설정하지 않은 것을 알아 냈다. myRow 클래스를 추가 한 후 완벽하게 작동했습니다. 도와 주셔서 감사합니다!

답변

1

문제는 myRow 클래스에있을 수 있습니다. 이 바인딩 된 행이 표시 원인

public class myRow { 
    public string key; 
    public string value; 

    public myRow(string Key, string Value) 
    { 
    key = Key; 
    value = Value; 
    } 

} 

하지만 텍스트가 아니 었 : 나는 당신의 코드를 재현하려고 할 때, 내가 먼저 "키"하고 그래서 myRow 클래스의 public 필드로 "값을"정의 세포. 내가 속성 둘 다 변경하는 경우, 훨씬 더 일 바인딩 :

public class myRow{ 
    private string _key; 
    public string key 
    { 
    get 
    { 
     return _key; 
    } 
    } 

    private string _value; 
    public string value 
    { 
    get 
    { 
     return _value; 
    } 
    } 

    public myRow(string Key, string Value) 
    { 
    _key = Key; 
    _value = Value; 
    } 

}

+0

이 문제는 해결되지 않았지만 내게 관심이있는 것은 이전 방법으로 표시되는 것을 보았다는 것입니다. 빈 격자가 표시됩니다. –

+0

DataGridView를 설정할 때 특별한 작업을 수행 했습니까? 방금 비주얼 디자이너에서 만들었습니다 –

+0

게시 한 그대로 코드가 실행되고 있습니까? 그렇다면 DataGridView에서 EndInit()에 대한 호출이 누락되었을 수 있습니다. – msergeant

1

은 아마 내가 코드에 만든 수정 할 수 있습니다. (필자는 DataTable을 사용하여 열을 만들고 행을 추가하는 부분에만 집중했습니다).

this.myData = new DataGridView(); 
((System.ComponentModel.ISupportInitialize)(myData)).BeginInit(); 

myData.Location = new System.Drawing.Point(12, 42); 
myData.Name = "myData"; 
myData.Size = new System.Drawing.Size(1060, 585); 
myData.TabIndex = 32; 

foreach (XElement xElem in xInfoItems) 
{ 
    numItems++; 
} 


// Here we create a DataTable with two columns. 
DataTable table = new DataTable(); 
table.Columns.Add("Key", typeof(string)); 
table.Columns.Add("Value", typeof(string)); 

foreach (XElement xElem in xInfoItems) 
{ 
    //Here we add rows to table 
    table.Rows.Add(xElem.Attribute("key").Value, xElem.Value); 
} 

myData.DataSource = table; 

myData.Refresh(); 

this.PerformLayout(); 
관련 문제