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

C# 无法在WinForm应用程序上显示数据集,c#,winforms,web-services,dataset,C#,Winforms,Web Services,Dataset,我的问题是无法在客户端windows窗体上查看数据集。我正在使用SetDataBinding方法,但我得到一个错误,即: 错误:“Systems.Windows.Forms.DataGridView”不包含SetDataBinding的任何定义,并且没有接受第一类参数的扩展方法“ 以下是返回数据集的web服务代码: public class Service1 : System.Web.Services.WebService { [WebMethod] public Dat

我的问题是无法在客户端windows窗体上查看数据集。我正在使用SetDataBinding方法,但我得到一个错误,即:

错误
“Systems.Windows.Forms.DataGridView”不包含SetDataBinding的任何定义,并且没有接受第一类参数的扩展方法“

以下是返回数据集的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;
          }
      }
   }

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

datagrid.DataSource = ds;
或者:

datagrid.DataMember = "TableName";

另外,对于web服务来说,数据集是一个可怕的选择。

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

datagrid.DataSource = ds;
或者:

datagrid.DataMember = "TableName";
另外,数据集对于web服务来说是一个可怕的选择。


将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];

另一个变化:
在您的Web服务中,将“从checkrecord中选择*,其中rollno=@rollno”此行更改为“从checkrecord中选择*,其中rollno=\'”+rollno+“\'”;
将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];

另一个变化:


在您的Web服务中,将“从checkrecord中选择*,其中rollno=@rollno”;此行更改为“”从checkrecord中选择*,其中rollno=\'”+rollno+“\'";

我尝试了,错误消失了,我得到了带有DataGridView和命令按钮的windows窗体,但当我单击按钮时,我看不到屏幕上的数据GridView@parth您是否设置了数据成员?实际上,将数据源设置到表中可能更容易,因为对于web服务来说,
数据集也是一个可怕的选择。仍然无法在我的DataGridView上查看数据集。还有其他更好的方法吗?更好的方法是不返回数据集。相反,返回POCO类(数据传输对象)的集合。我尝试了,我的错误消失了,我得到了带有DataGridView和一个命令按钮的windows窗体,但当我单击按钮时,我看不到屏幕上的数据GridView@parth您是否设置了数据成员?实际上,将数据源设置到表中可能更容易,因为对于web服务来说,
数据集也是一个可怕的选择。仍然无法在我的DataGridView上查看数据集。还有其他更好的方法吗?更好的方法是不返回数据集。相反,返回POCO类(数据传输对象)的集合。是的,我尝试了,我得到了这个错误:“Systems.Data.DataSet”不包含表的任何定义,并且没有接受第一类参数“ok.”的扩展方法。然后在DataSet ds=MyService.getdata(“abc”)上放置断点;行并检查您是否得到表?如果没有,则表示您的Web服务没有在数据集中返回任何表。这意味着您的查询没有输出。请在sql中运行此查询,并查看输出结果。@Parth_90:我想我找到了问题。.检查我的答案。我已编辑。我看到了sql查询的编辑和…我进行了重新编辑quired,在您提到的行再次插入断点..它说ds包含null:-(您能检查您提到的查询在sql中给出了什么输出吗?是的,我尝试了,我得到了这个错误:“Systems.Data.DataSet”不包含表的任何定义,并且没有接受第一类参数的扩展方法”确定..然后将断点放在数据集ds=MyService.getdata(“abc”)上;行并检查您是否得到表?如果没有,则表示您的Web服务没有在数据集中返回任何表。这意味着您的查询没有输出。请在sql中运行此查询,并查看输出结果。@Parth_90:我想我找到了问题。.检查我的答案。我已编辑。我看到了sql查询的编辑和…我进行了重新编辑要求在您提到的行再次插入断点。它说ds包含null:-(您能检查您提到sql中的输出的查询吗?仅供参考,该语言名为“C#”,而不是“C#.NET”。很抱歉,以后会记住它。谢谢:-)仅供参考,该语言名为“C#”,而不是“C#.NET”.对不起,我以后会记住的..谢谢:-)