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 ...
随机推荐
- 2019-08-23 纪中NOIP模拟A组
T1 [JZOJ2908] 矩阵乘法 题目描述 给你一个 N*N 的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第 K 小数. 数据范围 对于 $20\%$ 的数据,$N \leq 100$,$Q ...
- Cow Contest POJ - 3660 floyd传递闭包
#include<iostream> #include<cstring> using namespace std; ,INF=0x3f3f3f3f; int f[N][N]; ...
- Hdu2097 Sky数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2097 Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶 ...
- 使用python 写一个 股票涨跌提醒功能
1: 安装python: https://www.cnblogs.com/liyafei/articles/11550979.html 将python.exe加入 全局path 2: 安装库:(不需要 ...
- js上传文件工具类
个人博客 地址:http://www.wenhaofan.com/article/20180808210417 jQuery.extend({ uploadUtil:function(){ } }); ...
- spring aop使用,spring aop注解,Spring切面编程
================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...
- Sql 语句常语法
以前感觉在这个方面很欠缺,于是就找了些这方面的材料,自己也做了些总结,汇总到了一块.便于以后的查阅. --1.获取表的主键字段SELECT name FROM SysColumns WHERE id= ...
- Android 开发 assets和raw
在Android Project中,有两个文件夹的数据是不会被编译,以原型的方式打包到APK中,这两个文件夹就是 assets 和 res/raw/ 相同点: 1.数据不会编译成二进制字节码. 2.可 ...
- 基于pyqt5的图片素材批量处理工具
功能 分辨率的批量转换,文件夹递归查找 像素偏移量批量调整,文件夹单层查找 画布的大小的批量进行调整,不进行缩放,文件夹单层查找 界面 通过PyUIC生成的代码 # -*- coding: utf-8 ...
- [学习笔记]用Python简易向喜欢的人表白
前几天是情人节,就用Python图像库PIL来搞点事情. 先看图: 其实这样看不出什么来,然后需要放大: 放大以后就能看到你相对女神说的话. 但是对于学计算机的我来说,更想琢磨是怎样的流程完成的这个图 ...