WinForm中DataReader绑定到DataGridView的两种方法

 


在WinForm中,DataReader是不能直接绑定到DataGridView的,我想到了用两种方法来实现将DataReader绑定到DataGridView。

SqlCommand command = new SqlCommand(QueryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();

        //方法一,借助于DataTable
        //DataTable table = new DataTable();
        //table.Load(reader);
        //dataGridView1.DataSource = table;

        //方法二,借助于BindingSource
        BindingSource bs = new BindingSource();
        bs.DataSource = reader;
        dataGridView1.DataSource = bs;

        reader.Close();

 

posted on 2014-03-24 21:02  nanphon  阅读(702)  评论(0)    收藏  举报

导航