c# easyui 赋值_C# DataGridView绑定数据源的方法
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定。
1. 简单的数据绑定
例1
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString()))
{
SqlDataAdapter sda = new SqlDataAdapter("Select * From T_Class Where F_Type='Product' order by F_RootID,F_Orders", conn);
DataSet Ds = new DataSet();
sda.Fill(Ds, "T_Class");
//使用DataSet绑定时,必须同时指明DateMember
this.dataGridView1.DataSource = Ds;
this.dataGridView1.DataMember = "T_Class";
//也可以直接用DataTable来绑定
this.dataGridView1.DataSource = Ds.Tables["T_Class"];
}
简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性。
采用如下形式进行绑定:引用控件.DataBindings.Add("控件属性", 实例对象, "属性名", true);
例2
从数据库中把数据读出来放到一个数据集中,比如List<>、DataTable,DataSet,我一般用List<>,
然后绑定数据源:
IList sList=StudentDB.GetAllList();
DataGridView.DataSource=sList;
如果你没有设置DataGridView的列,它会自动生成所有列。
2. 复杂数据绑定
复杂的数据绑定是将一个以列表为基础的用户控件(例如:ComboBox、ListBox、ErrorProvider、DataGridView等控件)绑定至一个数据对象的列表。
基本上,Windows Forms的复杂数据绑定允许绑定至支持IList接口的数据列表。此外,如果想通过一个BindingSource组件进行绑定,还可以绑定至一个支持IEnumerable接口的数据列表。
对于复杂数据绑定,常用的数据源类型有(代码以DataGridView作为示例控件)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace DataGridViewBindingData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//this.dataGridView1.DataSource = DataBindingByList1();
//this.dataGridView1.DataSource = DataBindingByList2();
//this.dataGridView1.DataSource = DataBindingByDataTable();
this.dataGridView1.DataSource = DataBindingByBindingSource();
}
///
/// IList接口(包括一维数组,ArrayList等)
///
///
private ArrayList DataBindingByList1()
{
ArrayList Al = new ArrayList();
Al.Add(new PersonInfo("a","-1"));
Al.Add(new PersonInfo("b","-2"));
Al.Add(new PersonInfo("c","-3"));
return Al;
}
///
/// IList接口(包括一维数组,ArrayList等)
///
///
private ArrayList DataBindingByList2()
{
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
list.Add(new DictionaryEntry(i.ToString(),i.ToString()+"_List"));
}
return list;
}
///
/// IListSource接口(DataTable、DataSet等)
///
///
private DataTable DataBindingByDataTable()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Value");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
for (int i = 1; i <= 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i.ToString() + "_DataTable";
dt.Rows.Add(dr);
}
return dt;
}
///
/// IBindingListView接口(如BindingSource类)
///
///
private BindingSource DataBindingByBindingSource()
{
Dictionary dic = new Dictionary();
for (int i = 0; i < 10; i++)
{
dic.Add(i.ToString(),i.ToString()+"_Dictionary");
}
return new BindingSource(dic,null);
}
}
}
上面代码中BindingSource的Datasource是一个结构类型DictionaryEntry,同样的DictionaryEntry并不能直接赋值给Combobox的DataSource,但通过BindingSource仍然可以间接实现。 这是因为:
BindingSource可以作为一个强类型的数据源。其数据源的类型通过以下机制之一固定。使用 Add 方法可将某项添加到 BindingSource 组件中。
将 DataSource 属性设置为一个列表、单个对象或类型。(这三者并不一定要实现IList或IListSource)
这两种机制都创建一个强类型列表。BindingSource 支持由其 DataSource 和 DataMember 属性指示的简单数据绑定和复杂数据绑定。
总结:
根据DataSource绑定的对象的不同,可以有一下几种简单的绑定:
// DataSet 、DataTable
// 方式1
DataSet ds=new DataSet ();
this.dataGridView1.DataSource=ds.Table[0];
this.dataGridView1.DataSource = ds.Tables["表名"];
// 方式2
DataTable dt=new DataTable();
this.dataGridView1.DataSource=dt;
// DataView
DataView dv = new DataView();
this.dataGridView1.DataSource = dv;
// 设置了DataMember
DataSet ds=new DataSet ();
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "表名";
// ArrayList
ArrayList Al = new ArrayList();
this.dataGridView1.DataSource = Al;
// dic
Dictionary dic = new Dictionary();
this.dataGridView1.DataSource = dic;
// List
this.dataGridVi.DataSource = new BindingList(List);
3. 实例
3.1 手动给dataGridView绑定数据源的方法
C#中手动给dataGridView绑定数据源,能够很自由地进行操作,但展示数据并没有C#自动添加数据源那么方便。可有时为了方便操作数据,我们更愿意手动连接数据源,代码如下:
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Restaurant.mdb");//建立数据库连接
cmd = new OleDbCommand("select * from data", conn);//执行数据连接
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];//数据源
this.dataGridView1.AutoGenerateColumns = false;//不自动
conn.Close();//关闭数据库连接
说明:解决DataGridView绑定了数据源无法更新保存当前行的问题
this.dataGridView.currentCell=null;//该行的作用是取消datagridview行的编辑状态
adapter.Update(userTable);
3.2 利用泛型集合向DataGridView中添加数据
List<>泛型集合:
private void Form1_Load(object sender, EventArgs e)
{
//使用List<>泛型集合填充DataGridView
List students = new List();
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<>泛型集合
private void Form1_Load(object sender, EventArgs e)
{
//使用Dictionary<>泛型集合填充DataGridView
Dictionary students = new Dictionary();
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;
}
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];
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
c# easyui 赋值_C# DataGridView绑定数据源的方法相关推荐
- 章鱼哥出品—VB.NET DataGridView绑定数据源 quot;与货币管理器的位置关联的行不能设置为不可见quot; 问题的解决...
DtaGridView绑定数据源后.假设想让数据条件显示的话,直接使用 My_Row.Visible = False就会出错.错误类型是 "与货币管理器的位置关联的行不能设置为不可见&qu ...
- DataGridView绑定数据源后添加行
在已经绑定数据源时,无法以Add的方式方式添加行,会报错 解决方法一: DataRow dr =((DataTable)dataGridView1.DataSource).NewRow; ((Data ...
- datagridview绑定数据源不显示_sharding-jdbc系列之 数据源配置(一)
spring boot Yaml方式 @Bean 定义一个Config类,配置数据源,上面的代码很简单,无非就是获取yaml文件,然后通过YmlByteArrayDataSource创建一个dataS ...
- Winform中实现List<string>赋值给dataGridView与实现多选、全选和获取选择的内容
场景 Winform中给DataGridView添加多选框列并获取选中行的内容: Winform中给DataGridView添加多选框列并获取选中行的内容_BADAO_LIUMANG_QIZHI的博客 ...
- Winfrom DataGridView DataGridViewComboBoxCell 数据源绑定
Winfrom DataGridView DataGridViewComboBoxCell 数据源绑定 解决方案: 1.先给数据源赋值 List<BindDataModel> Proces ...
- 关于DataGridView的数据源绑定字符串两个值得注意的问题
1. LINQ的查询结果无法直接作为DataGridView的数据源 DataGridView的DataSource属性为object类型,但并不意味着任何类型都可以作为DataGridView的数据 ...
- C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题
C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题 参考文章: (1)C# Winfrom DataGridView DataSource绑定数据源后-- ...
- datagridview的数据源的操作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- DataGridView绑定对象数组 c# 1614236580
DataGridView绑定对象数组 c# 1614236580 参考代码 1,控件绑定数组 2,指定列的数据源 为 对象的属性名称
最新文章
- 武汉第二中学2021年高考成绩查询,武汉中学排名前十名,2021年武汉中学排名一览表...
- 数据分析如何用好高可用?Naming双活必须掌握!
- numpy.ndarray.view()(懵逼,看不太懂???)(view不会开辟新的内存空间)
- CMFCPropertySheet的使用及PROPSHEETHEADER结构体介绍
- ASP .NET 如何在 SQL 查询层面实现分页 1
- [GZOI2016] 亚索的量子实验【分块】
- Java性能优化的七个方向
- 实现通用人工智能和超(强)人工智能的理论基础——心理二元说
- Mac,Windows11,Windows10局域网互传共享文件
- 统计(statistic)(二分查找+离散化)
- Google Earth Engine——美国人口数据可视化分析
- 查看远程计算机ip地址吗,我的电脑跟别人远程过可不可以查对方IP地址
- Linux监控利器nagios–NSCA被动监控
- 如何创建PostgreSQL 生成列
- 仿soul交友盲盒1.0全开源源码
- cmd ping 一台计算机名,windows CMD命令查看局域网内所有主机名及IP
- 记录:微信小程序Switch样式调整
- FOFA网络空间搜索引擎
- android 充电动画_酷玩儿丨去掉耳机和充电器,以后数据线苹果也不会送了
- 【转载】通过JSFL让Flash Professional CS4或CS5拥有批量FLA导出SVG的功能
热门文章
- 我们还没有进入元宇宙吗?
- 基于android系统的音乐播放器,基于Android系统的音乐播放器软件设计与实现.doc
- MAC个人推荐的3款思维导图APP
- Struts2——整理好的学习资料
- java代码ping服务IP工具类(Windows,Linux环境)
- 前端重构方案了解一下
- 百家号自媒体过新手期要多久?
- matlab解决迷宫问题,用matlab处理蚂蚁迷宫问题
- 零售业如何抓住机遇?“主动出击”才是首选
- 2D+1D | vivo官网Web 3D应用开发与实战