2012-04-11 102 views
3

問題是從我的web服務中獲取dataGridView中的數據。當我從web服務調用getList方法時,我得到正確的xml代碼。 這是我的代碼: Web服務:將數據導入到dataGridView

[WebMethod] 
    public DataSet getList() 
    { 
     SqlConnection connection = new SqlConnection(); 
     connection.ConnectionString = "server=localhost;" + 
            "Trusted_Connection=yes;" + 
            "database=oving1; " + 
            "connection timeout=30"; 
     string select = "select * from Person"; 
     SqlDataAdapter da = new SqlDataAdapter(select, connection); 
     DataSet ds = new DataSet(); 

     da.Fill(ds, "Person"); 
     return (ds); 
    } 

形式:

private void button1_Click(object sender, EventArgs e) 
    { 

     Service1 webService = new Service1(); 
     DataSet ds = webService.getList(); 
     dataGridView1.DataSource = ds; 


    } 
+0

嘗試將ds填充到數據表中,然後將此錶鏈接到網格視圖。 – Milee 2012-04-11 09:00:18

+0

是否按預期填充了「ds」?使用斷點。 – SkonJeet 2012-04-11 09:00:45

+2

嘗試dataGridView1.DataSource = ds.Tables [0],也請確保你沒有將dataGridView1的autogeneratedcolumns設置爲false – Habib 2012-04-11 09:04:06

回答

3

您最好檢查一下這個Avoiding DataSet in Web services

要序列數據集在Web服務Consuming a DataSet from an XML Web Service

一件事嘗試將數據表格傳遞給網格而不是數據集:

dataGridView1.DataSource = ds.Tables[0]; 
+1

@stianboe也請投票正確答案 – Ravia 2012-04-11 11:12:47

+0

@Ravia,謝謝,但stianboe有13分,他需要2點以上才能投票;) – 2012-04-11 12:10:13

+0

@stianboe我想現在你可以投票了 – Ravia 2012-04-11 13:14:44