加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将DataGridView绑定到DataSet会直接失败

发布时间:2020-12-16 01:39:03 所属栏目:百科 来源:网络整理
导读:由此代码绑定的DataGridView不会按预期显示信息: dataGridView1.DataSource = ds; 这是ds的代码: public DataSet ConnectandReadList() { DataSet ds = new DataSet(); string connection_string="Data Source=hermes;database=qcvalues; Integrated Secur
由此代码绑定的DataGridView不会按预期显示信息:

dataGridView1.DataSource = ds;

这是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(),myConnection);
                var adapter = new SqlDataAdapter(command);

                adapter.Fill(ds);
            }


            return ds;
        }

解决方法

尝试绑定到数据集内的表:dataGridView1.DataSource = ds.Tables [0];

从DataGridView.DataSource属性的文档中,您还可以绑定到DataSet并使用DataMember属性:

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.

我认为在这种情况下,表名称将是“表”,因为您没有明确地命名它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读