【问题标题】:How to do databind to devexpress gridview using C# code on winforms app?如何在winforms应用程序上使用C#代码对devexpress gridview进行数据绑定?
【发布时间】:2018-01-17 20:07:22
【问题描述】:

我是winforms的新手。我正在尝试调用存储过程并显示到 devexpress gridview 控件中

这是我尝试过的代码

//Header file
using System.Data;
using System.Data.SqlClient;
using System.Configuration;



public Form3()
    {
        InitializeComponent();

        //string cs = Properties.Settings.TestDatabaseConnectionString;
        var connectionString = ConfigurationManager.ConnectionStrings["TestDatabaseConnectionString"].ConnectionString;

        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = connectionString;

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "ReportStudentDetails";
            cmd.Connection = con;

            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            gridControl1.DataSource = ds;
            gridControl1.DataBind(); // showing error
            con.Close();

        }

    }

错误:

GridControl1 不包含数据绑定的定义

【问题讨论】:

  • 删除gridControl1.DataBind(); 行。 gridControl1.DataSource = ds; 就足够了。
  • 另外,ASP.NET 和 WinForms 是不兼容的技术。你是如何一起使用它们的?
  • @Gosha_Fighten 正如你提到的,我删除了。现在没有错误。但同时它没有显示任何记录
  • 将网格数据源设置为您的 DataSet 所需的表。例如:gridControl1.DataSource = ds.Tables[0];
  • @Gosha_Fighten Bro 你是对的,你能否详细解释一下你的答案,这对其他人也会更有帮助

标签: c# asp.net winforms gridview devexpress


【解决方案1】:

DataBind 方法特定于 ASP.NET 控件。在 WinForms 中,通过设置控件属性来跟踪所有内容。所以,没有必要使用DataBind方法。

那么,DataSet 不是可绑定对象。它包含此类对象的集合。它们是数据表。因此,有必要将 DataSource 属性设置为 DataSet 表之一。例如,gridControl1.DataSource = ds.Tables[0];。或者,如果您已命名表,gridControl1.DataSource = ds.Tables['myTableName'];。或者,您可以使用 GridControl DataMember 属性来指定表名。

gridControl1.DataSource = ds;
gridControl1.DataMember = "myTableName";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2012-04-26
    • 2014-02-20
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多