0

CSV 파일을 읽고 처리 한 후 모바일 응용 프로그램에 DataGrid을 표시하려고합니다. 여기에 내가 지금까지 가지고있는 것입니다 :DataGrid에 데이터가 표시되지 않습니다.

private void btOpenFile_Click(object sender, EventArgs e) 
{ 
    try 
    {   
     // Process all data into an array of Object 
     // this.records array contains objects of type MyRecord 

     // Create datatable and define columns 
     DataTable dt = new DataTable("myDt"); 
     dt.Columns.Add(new DataColumn("String A",typeof(string))); 
     dt.Columns.Add(new DataColumn("Int 1", typeof(int)));     

     // Loop through and create rows 
     foreach(MyRecord record in records) { 
      DataRow row = dt.NewRow();       
      row[0] = record.stringA; 
      row[1] = record.int1; 
      dt.Rows.Add(row); 
     } 

     // Create dataset and assign it to the datasource.. 
     DataSet ds = new DataSet("myDs"); 
     ds.Tables.Add(dt);           
     dataGrid.DataSource = ds; 
     dataGrid.Refresh(); 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error: " + ex.Message,"Error"); 
    } 
} 

내 응용 프로그램을 실행할 때 빈 데이터 격자 구성 요소가 있습니다. 누군가 내 실수를 지적 할 수 있습니까? 또는이를 올바르게 수행하는 방법은 무엇입니까?

답변

1

데이터 집합없이 시도하십시오. dataGrid.DataSource = dt; 대신

+0

고마워. 하지만 데이터 세트를 사용하여 보여주는 예제가 있습니다. 그것이 왜 효과가 없었던 모든 추론? –

0

:

dataGrid.DataSource = ds;   
dataGrid.Refresh(); 

시도 :

dataGrid.SetDataBinding(ds, "myDt"); 

이 동시에 데이터 소스 및 DataMember를 설정하는 효과가 있습니다.

DataSet Ds = new DataSet(); 
DataTable Dt = new DataTable(); 

Ds.Tables.Add(Dt); 
Dt.Columns.Add(new DataColumn("String A", typeof(string)));     
Dt.Columns.Add(new DataColumn("Int 1", typeof(int)));  

Dt.Rows.Add(new object[] { "Patricia", 3 }); 
Dt.Rows.Add(new object[] { "John", 4 }); 
Dt.Rows.Add(new object[] { "Mayer", 5 }); 
1

... ... 여기 그것은 예를 들어

입니다
Dataset DS = new Dataset(); 
DS.Tables.Add(TableName); 

// 
Populate dataset 
// 

mydatagrid.Datasource = DS.Tables[0]; 

그래서 짧은 당신이 데이터를 표시하는 데이터 세트 내에서 테이블을 참조 할 필요가 ... 희망이 도움이 ... :)

0

문제는 당신은 데이터 세트로는 Dataset.Datatable 같은해야하는 데이터 소스를 참조 할 수 없습니다입니다 그것은 도움이 될이 시도

+0

Plz 표시로 대답하면 ... –

0

그것을 당신은뿐만 아니라 데이터 집합 내부의 테이블에 데이터 소스를 설정하면 잘 작동합니다 .. 뭔가 같은 :

datagrid.DataSource = ds.Tables["myDt"]; 

데이터 세트는 하나 개 이상의 테이블을 포함 할 수 있음을 기억하고 우리가 사용하는 어느 명시 적으로 말할 필요 어디서 볼 수 있습니다 :

HTH 내가 대답하기는 매우 늦게 알고 ....하지만 다른 사람에게 도움이 될 것입니다

-1

...

DataSet ds = gridUpdate.GridUpdate(); 

(worr 해달라고 gridUpdate.GridUpdate()에 대한 내 데이터 집합을 만드는 내 함수)

dataGridView1.DataSource = ds.Tables[0]