无法在WinForm应用程序上显示数据集

时间:2011-11-13 13:07:35

标签: c# winforms web-services dataset

我的问题是我无法在客户端Windows窗体上查看DataSet。我正在使用SetDataBinding方法,但我得到一个错误说:

错误"Systems.Windows.Forms.DataGridView" doesn't contain any defination for SetDataBinding and no extension method accepting first type of argument"

以下是返回数据集的Web服务代码:

  public class Service1 : System.Web.Services.WebService
  {
    [WebMethod]
    public DataSet getdata(String rollno)
    {
        try
        {
            using (SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
            {
                string select = "select * from checkrecord where rollno=@rollno";
                SqlDataAdapter da = new SqlDataAdapter(select, myConnection);
                DataSet ds = new DataSet();
                da.Fill(ds, "students");
                return (ds);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;                
        }

    }

绑定数据的客户端代码:

      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.Web.Services;
      using System.Web.Services.Protocols;
      using WindowsFormsApplication1.dataset1;

     //dataset1 is my web reference name
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      { 
          public Form1()
          {
              InitializeComponent();
          }
          private void calldatagrid_Click(object sender, EventArgs e)
          {
             Service1 MyService = new Service1();
            // Call the GetData method in the Web Service
            DataSet ds = MyService.getdata("abc");
            // Bind the DataSet to the Grid
            datagrid.SetDataBinding=ds;
          }
      }
   }

2 个答案:

答案 0 :(得分:2)

SetDataBinding是在DataGrid上定义的,而不是DataGridView(不相关的),并且是一个方法而不是属性,并且需要两个参数。请尝试改为:

datagrid.DataSource = ds;

并且可选:

datagrid.DataMember = "TableName";

暂时不谈......数据集对于网络服务来说是一个可怕的可怕选择。

答案 1 :(得分:2)


datagrid.SetDataBinding = ds; 行更改为 datagrid.DataSource = ds.Table [0];

使用下面的代码:

 Service1 MyService = new Service1();
 // Call the GetData method in the Web Service
  DataSet ds = MyService.getdata("abc");
 // Bind the DataSet to the Grid
  datagrid.DataSource=ds.Table[0];


另一个变化:
在您的网络服务中更改“select * from checkrecord where rollno = @ rollno ”; 此行改为“select * from checkrecord rollno = \'” + rollno + “\”“;