我的问题是我无法在我的客户端窗口窗体上查看数据集。我正在使用 SetDataBinding 方法,但我收到一条错误消息:
错误:“Systems.Windows.Forms.DataGridView”不包含 SetDataBinding 的任何定义,也没有接受第一种参数类型的扩展方法”
这是返回数据集的网络服务代码:
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;
}
}
绑定(bind)数据的客户端代码:
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;
}
}
}
请您参考如下方法:
将 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 where rollno=\'"+rollno+"\'";
