在Winform中向DataGridView控件添加数据的三种方式
2017-05-31 20:46
615 查看
在Winform中向DataGridView控件添加数据很常用到,现总结3种填充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
关键代码:
//使用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;
}
备注:在很多情况下,BindingSource对象起到一个过渡的作用,因为SqlDataReader对象直接赋给DataGridView
时,不能正常显示数据,所以利用BindingSource对象做一个绑定。
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");
b17e
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;
}
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
关键代码:
//使用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;
}
备注:在很多情况下,BindingSource对象起到一个过渡的作用,因为SqlDataReader对象直接赋给DataGridView
时,不能正常显示数据,所以利用BindingSource对象做一个绑定。
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");
b17e
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;
}
相关文章推荐
- 在Winform中向DataGridView控件添加数据的三种方式
- c# Form中向DataGridView控件添加数据的三种方式
- 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- Winform中向DataGridView控件添加数据
- 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。
- c#当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- 地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)
- “当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。” 的解决一法
- VB 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- 关于WinForm中的DataGridView控件显示数据字典的解决方案。
- C#给WinForm(窗体)下DataGridView控件添加单选选或多选按钮列
- WinForm中利用DataSet向DataGridView中添加多行数据
- DataGridView数据绑定控件---------按钮添加和事件触发总结
- Winform中listView控件从数据库中添加数据的方法
- winform窗体添加背景音乐(三种方式)
- Visual C# 2005 - 如何于DataGridView控件中以跨数据行方式显示数据
- 如何于DataGridView控件中以跨数据行方式显示数据
- winform,DataGridView绑定数据后更改列属性、动态创建(用一个控件显示多表适用)
- 如何于DataGridView控件中以跨数据行方式显示数据