Form DataGridView绑定BindingSource的几种方式
本文链接:https://blog.csdn.net/qq_15138169/article/details/83341076
在WinForm的开发中,ListView和DataGridView应用的场景都比较,初学的时候绑定数据都是用最简单的一种方式去实现
- private void NormalAdd()
- {
- dataGridView1.Rows.Clear();
- for (int i = 0; i < 10; ++i)
- {
- int index = dataGridView1.Rows.Add();
- dataGridView1.Rows[index].Cells[0].Value = i+"elem1";
- dataGridView1.Rows[index].Cells[1].Value = i+"elem2";
- dataGridView1.Rows[index].Cells[2].Value = i+"elem3";
dataGridView1.Rows[index].Cells[2].Value = i+"elem4";
上面的方法其实是最直观的,但是微软还提供其他几种比较优雅一些的方式去绑定数据,就是通过BindingSource来实现,BindingSource看名字就知道干啥用的了。
public List<ItemBean> list = new List<ItemBean>();
private void dataList()
{
for (int i = 0; i < 10; ++i)
{
ItemBean item = new ItemBean();
item.postion = i.ToString();
item.item1 = "Listitem1-" + i;
item.item2 = "Listitem2-" + i;
item.item3 = "Listitem3-" + i;
list.Add(item);
}
bindingSource1.DataSource = list;
//DataGridView的列name和对象成员的绑定
dataGridView1.Columns["Column1"].DataPropertyName = "postion";
dataGridView1.Columns["Column2"].DataPropertyName = "item1";
dataGridView1.Columns["Column3"].DataPropertyName = "item2";
dataGridView1.Columns["Column4"].DataPropertyName = "item3";
}
public class ItemBean
{
public string postion { get; set; }
public string item1 { get; set; }
public string item2 { get; set; }
public string item3 { get; set; }
}
这种是List数据源方式实现绑定
public DataTable dt = new DataTable("ITEMBEAN");
private void dataTable()
{
dt.Columns.Add("POS", typeof(string));
dt.Columns.Add("ITEM1", typeof(string));
dt.Columns.Add("ITEM2", typeof(string));
dt.Columns.Add("ITEM3", typeof(string));
for (int i = 0; i < 10; ++i)
{
DataRow dr = dt.NewRow();
dr[0] = i.ToString();
dr[1] = i.ToString() + "_Table单元1";
dr[2] = i.ToString() + "_Table单元2";
dr[3] = i.ToString() + "_Table单元3";
dt.Rows.Add(dr);
}
bindingSource1.DataSource = dt;
dataGridView1.Columns["Column1"].DataPropertyName = "POS";
dataGridView1.Columns["Column2"].DataPropertyName = "ITEM1";
dataGridView1.Columns["Column3"].DataPropertyName = "ITEM2";
dataGridView1.Columns["Column4"].DataPropertyName = "ITEM3";
}
这个的是DataTable的方式,其他的还有几种,大家可以自己去试一试,这两种的在实际项目中应用场景应该比较多一些。
Form DataGridView绑定BindingSource的几种方式的更多相关文章
- DataGridView绑定数据源的几种方式
使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...
- 【转】DataGridView绑定数据源的几种方式
第一种:DataSet ds=new DataSet (); this.dataGridView1.DataSource=ds.Table[0]; 第二种:DataTable dt=new DataT ...
- C# DataGridView绑定数据源的几种常见方式
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...
- Android_安卓为按钮控件绑定事件的五种方式
一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick ...
- Form 表单提交的几种方式
简单的总结一下form表单提交的几种方式:1.最简单的方式 就用form的submit提交方式,这种提交方式是不需要回调函数的 这种方式最近到一个form提供action路径后台接受就可以< ...
- 为input标签绑定事件的几种方式
为input标签绑定事件的几种方式 1.JavaScript原生态的方式,直接复制下面的代码就会有相应的效果 <!DOCTYPE html><html><head> ...
- angular学习笔记(三)-视图绑定数据的两种方式
绑定数据有两种方式: <!DOCTYPE html> <html ng-app> <head> <title>2.2显示文本</title> ...
- JS与JQ绑定事件的几种方式.
JS与JQ绑定事件的几种方式 JS绑定事件的三种方式 直接在DOM中进行绑定 <button onclick="alert('success')" type="bu ...
- jQuery绑定事件的四种方式:bind、live、delegate、on
1.jQuery操作DOM元素的绑定事件的四种方式 jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undele ...
随机推荐
- The Number of Inversions(逆序数)
For a given sequence A={a0,a1,...an−1}A={a0,a1,...an−1}, the number of pairs (i,j)(i,j) where ai> ...
- 免费馅饼 HDU - 1176 基础dp
/*题都是有一个状态转移方程式 , 只要推出方程式就问题不大了,首 先对于gameboy来说他下一秒只能 在0~10这十一个位置移动, 而对于1~9这九个位置来说他可以移动(假设他现在的位置为x)到x ...
- 数据预处理 | python 第三方库 imblearn 处理样本分布不均衡问题
说明:目前 只记录了 过采样 和 欠采样 的代码部分 1 样本分布不均衡描述: 主要出现在与分类相关的建模问题上,不均衡指的是不同类别的样本量差异非常大. 样本量差距过大会影响到建模结果 2 出现的场 ...
- Android 开发 facebook分享,登陆,获取信息
1 搭建开发环境 1.1 在Facebook官网SDK中,下载4.0.0的SDK包. 1.2 使用Eclipse导入SDK包中的Facebook工程,并添加android-supp ...
- Session方法
Session的save()和persist()方法Session的save()方法使一个临时对象转变为持久化对象.它完成以下操作:(1)将临时对象加入到Session缓存中,使其进入持久化状态.(2 ...
- LINUX使用SSH远程终端时,如何将运行时间长的程序在后台挂起,下次SSH登陆时继续使用同一个SHELL?
我在某个平台上购买了一个云服务器,LINUX操作系统无图形化界面,硬盘空间较小.虽然在平台上可以通过其自带网页版VNC界面登陆SHELL进而操控云主机,但是每次需要操控都得打开网页登陆进平台,然后再进 ...
- IntelliJ IDEA构建多Module项目
打开IDEA 创建完成项目后,我们创建子模块 可以看到common子模块创建成功,子模块的名字大家可以根据自己的实际需求来修改 下面我们再创建子模块 给子模块起个名字 现在已经创建好多模块的项目了,下 ...
- ansible笔记(13):变量(二)
1.谈一谈[Gathering Facts]:使用setup模块查看 当我们运行一个playbook时,默认都会运行一个名为“[Gathering Facts]”的任务,前文中已经大致的介绍过这个默认 ...
- 【Python】循环控制保留字
- jQuery func
1. $(selector).each(function(index,element)); -----------index 选择器的index位置,element --当前的元素 2. _.eac ...