C# DataGridView绑定数据源的方法

2020-01-05 09:35:23于海丽

3.3 利用SqlDataReader填充DataGridView 


//使用SqlDataReader填充DataGridView 
using (SqlCommand command = new SqlCommand("select * from product", DBService.Conn)) 
{ 
   SqlDataReader dr = command.ExecuteReader(); 
   BindingSource bs = new BindingSource(); 
   bs.DataSource = dr; 
   this.dataGridView1.DataSource = bs; 
}

3.4 利用SqlDataAdapter对象向DataGridView中添加数据 


using (SqlDataAdapter da = new SqlDataAdapter("select * from Product", DBService.Conn)) 
{ 
   DataSet ds = new DataSet(); 
   da.Fill(ds); 
   this.dataGridView1.DataSource = ds.Tables[0]; 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到c#教程频道。