c# Form中向DataGridView控件添加数据的三种方式
来源:互联网 发布:重启网络命令 编辑:程序博客网 时间:2024/06/14 20:04
c# Form中向DataGridView控件添加数据的三种方式
1.利用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];}
2. 利用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.利用泛型集合向DataGridView中添加数据
/*第一种泛型示例*/ //关键代码:(List<>泛型集合) private void Form1_Load(object sender, EventArgs e) { //使用List<>泛型集合填充DataGridView List<Student> students = new List<Student>(); Student hat = new Student("Hathaway", "12", "Male"); Student peter = new Student("Peter","14","Male"); Student dell = new Student("Dell","16","Male"); Student anne = new Student("Anne","19","Female"); students.Add(hat); students.Add(peter); students.Add(dell); students.Add(anne); this.dataGridView1.DataSource = students; }
/*第二种泛型示例*/ //关键代码:(Dictionary<>泛型集合,与List<>泛型集合略有不同) private void Form1_Load(object sender, EventArgs e) { //使用Dictionary<>泛型集合填充DataGridView Dictionary<String, Student> students = new Dictionary<String, Student>(); Student hat = new Student("Hathaway", "12", "Male"); Student peter = new Student("Peter","14","Male"); Student dell = new Student("Dell","16","Male"); Student anne = new Student("Anne","19","Female"); students.Add(hat.StuName,hat); students.Add(peter.StuName,peter); students.Add(dell.StuName,dell); students.Add(anne.StuName,anne); //在这里必须创建一个BindIngSource对象,用该对象接收Dictionary<>泛型集合的对象 BindingSource bs = new BindingSource(); //将泛型集合对象的值赋给BindingSourc对象的数据源 bs.DataSource = students.Values; this.dataGridView1.DataSource = bs; }
0 0
- c# Form中向DataGridView控件添加数据的三种方式
- 在Winform中向DataGridView控件添加数据的三种方式
- 在Winform中向DataGridView控件添加数据的三种方式
- c#当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- Winform中向DataGridView控件添加数据
- 向DataGridView控件添加数据
- 向DataGridView控件添加数据 .
- 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。
- 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。
- 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- VB 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- C#向excel中写入数据的三种方式
- C#中向DataGridView中添加DataTable数据
- “当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。” 的解决一法
- C#中DataGridView控件的数据绑定
- VB.NET逐行(一行一行)向DataGridView数据控件中添加数据
- DataGridView控件添加数据
- DataGridView控件添加数据
- Web应用架构
- POJ 3667 Hotel
- gcc: readline/libreadline.a: No such file or directory
- 需求评审五个维度框架分析及其带来的启示-3-典型需求评审
- JavaScript基础总结
- c# Form中向DataGridView控件添加数据的三种方式
- java7 新特性 Files copy
- 基础命令
- 使用Camera2 替代过时的Camera API
- csdn待改进点之4------>个人资料中的排版格式如此, 这样真的好吗?
- Sugar ORM 普及文章
- 新闻切换特效
- BZOJ3511 土地划分 题解&代码
- 51nod 1126 求递推序列的第N项