简体   繁体   中英

How to do databind to devexpress gridview using C# code on winforms app?

I am new to winforms. I am trying to call stored procedure and display into devexpress gridview control

Here is my code what I tried

//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();

        }

    }

Error:

GridControl1 Doesn't contain a definition of data bind

The DataBind method is specific to ASP.NET controls. In WinForms, everything is tracked by setting a control property. So, there is no need to use the DataBind method.

Then, DataSet is not bindable object. It contains a collection of such object. They are DataTables. So, it's necessary to set the DataSource property to one of the DataSet tables. For example, gridControl1.DataSource = ds.Tables[0]; . Or, if you have named tables, gridControl1.DataSource = ds.Tables['myTableName']; . Alternatively, you can use the GridControl DataMember property to specify the table name.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM