开发者

Binding a DataGridView to a DataSet directly fails

开发者 https://www.devze.com 2023-01-23 08:40 出处:网络
A DataGridView bound by this code does not display information as expected: dataGridView1.DataSource = ds;

A DataGridView bound by this code does not display information as expected:

dataGridView1.DataSource = ds;

here's the code for ds:

public DataSet ConnectandReadList()
        {
            DataSet ds = new DataSet();

            string connection_string="Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";            

            using (var myConnection = new SqlConnection(connection_string))
            {

                myConnection.Open();
                var command = new SqlCommand(InitializeQuery(), myCon开发者_高级运维nection);
                var adapter = new SqlDataAdapter(command);

                adapter.Fill(ds);
            }


            return ds;
        }


Try binding to the table inside the dataset: dataGridView1.DataSource = ds.Tables[0];

From the documentation for the DataGridView.DataSource property, you can also bind to a DataSet and use the DataMember property:

When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to.

I think in this case the table name would be "Table" since you are not naming it explicitly.


Is ds a DataSet?

If so, try setting your DGV's DataMember to a DataTable within the DataSet or specifying your DataSet's DataTable for the DataSource.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号