Невозможно отобразить набор данных в приложении WinForm - PullRequest
1 голос
/ 13 ноября 2011

Моя проблема в том, что я не могу просмотреть DataSet в форме окна клиента.Я использую метод SetDataBinding, но получаю сообщение об ошибке:

Ошибка : "Systems.Windows.Forms.DataGridView" doesn't contain any defination for SetDataBinding and no extension method accepting first type of argument"

Вот код веб-службы, который возвращает набор данных:

  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 ]

2 голосов
/ 13 ноября 2011


изменить 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 где rollno = @ rollno "; эта строка на "select * from checkrecord где rollno = \ '" + rollno + "\" ";

2 голосов
/ 13 ноября 2011

SetDataBinding определен в DataGrid, а не в DataGridView (не связан) и является методом, а не свойством, и требует двух параметров. Попробуйте вместо:

datagrid.DataSource = ds;

И дополнительно:

datagrid.DataMember = "TableName";

В качестве отступления ... Наборы данных - ужасный ужасный выбор для веб-сервисов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...