本文链接:https://blog.csdn.net/qq_15138169/article/details/83341076

在WinForm的开发中,ListView和DataGridView应用的场景都比较,初学的时候绑定数据都是用最简单的一种方式去实现

  1. private void NormalAdd()
  2. {
  3. dataGridView1.Rows.Clear();
  4. for (int i = 0; i < 10; ++i)
  5. {
  6. int index = dataGridView1.Rows.Add();
  7. dataGridView1.Rows[index].Cells[0].Value = i+"elem1";
  8. dataGridView1.Rows[index].Cells[1].Value = i+"elem2";
  9. 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的几种方式的更多相关文章

  1. DataGridView绑定数据源的几种方式

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

  2. 【转】DataGridView绑定数据源的几种方式

    第一种:DataSet ds=new DataSet (); this.dataGridView1.DataSource=ds.Table[0]; 第二种:DataTable dt=new DataT ...

  3. C# DataGridView绑定数据源的几种常见方式

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...

  4. Android_安卓为按钮控件绑定事件的五种方式

    一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick ...

  5. Form 表单提交的几种方式

    简单的总结一下form表单提交的几种方式:1.最简单的方式 就用form的submit提交方式,这种提交方式是不需要回调函数的   这种方式最近到一个form提供action路径后台接受就可以< ...

  6. 为input标签绑定事件的几种方式

    为input标签绑定事件的几种方式 1.JavaScript原生态的方式,直接复制下面的代码就会有相应的效果 <!DOCTYPE html><html><head> ...

  7. angular学习笔记(三)-视图绑定数据的两种方式

    绑定数据有两种方式: <!DOCTYPE html> <html ng-app> <head> <title>2.2显示文本</title> ...

  8. JS与JQ绑定事件的几种方式.

    JS与JQ绑定事件的几种方式 JS绑定事件的三种方式 直接在DOM中进行绑定 <button onclick="alert('success')" type="bu ...

  9. jQuery绑定事件的四种方式:bind、live、delegate、on

    1.jQuery操作DOM元素的绑定事件的四种方式 jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undele ...

随机推荐

  1. C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]

    构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...

  2. Common Subsequence POJ - 1458 最长公共子序列 线性DP

    #include <iostream> #include <algorithm> #include <string> #include <cstring> ...

  3. Mysql中FIND_IN_SET()和IN区别简析

    来源:http://www.jb51.net/article/125744.htm 测试SQL: CREATE TABLE `test` ( `id` int(8) NOT NULL auto_inc ...

  4. python package install error and little code bugs

    When you install packages using setup.py, the error: (py37) C:\Users\weda\Phd\python packages\visibi ...

  5. idea 配置 tomcat 教程

    最近在搞一个项目需要用到idea 配置tomcat,翻了翻网上的帖子发现稂莠不齐,最后决定还是自己写个吧!(其实我挺蠢的走了好多的弯路,哎~) 1.首先准备一个需要大家tomcat的工程,然后使用id ...

  6. 三、linux环境的搭建1(oracle、ssh、jdk、mysql、samba、tomcat)

    linux环境的搭建1(oracle.ssh.jdk.mysql.samba.tomcat)   网络配置 方案一 tip 1 使用ifconfig : ifconfig eth0 新ip 然后编辑/ ...

  7. Python获取当前文件路径及父文件路径

    import os # 当前文件的路径 1.os.getcwd(): 2.os.path.realpath(__file__) # 当前文件的父路径 1.pwd=os.getcwd()   os.pa ...

  8. soundtouch 变速算法matlab实现

    soundtouch变速主要采用WSOLA算法来进行变速. http://www.surina.net/soundtouch/ https://blog.csdn.net/suhetao/articl ...

  9. [HEOI2016] 字符串 - 后缀数组,主席树,ST表,二分

    [HEOI2016] 字符串 Description 给定一个字符串 \(S\), 有 \(m\) 个询问,每个询问给定参数 \((a,b,c,d)\) ,求 \(s[a..b]\) 的子串与 \(s ...

  10. PP: UMAP: uniform manifold approximation and projection for dimension reduction

    From Tutte institute for mathematics and computing Problem: dimension reduction Theoretical foundati ...