2012-04-11 99 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

相关问题