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 ...
随机推荐
- [Code+#4] 最短路 - 建图优化,最短路
最短路问题,然而对于任意\(i,j\),从\(i\)到\(j\)可以只花费\((i xor j) \cdot C\) 对每个点\(i\),只考虑到\(j\)满足\(j=i xor 2^k, j \le ...
- SmartSVN:File has inconsistent newlines
用SmartSVN提交文件的时候,提示svn: File has inconsistent newlines 这是由于要提交的文件编码时混合了windows和unix符号导致的. 解决方案 Smart ...
- 番外:你真的了解 Oracle 的启动流程吗?
番外系列说明:该系列所有文章都将作为独立篇章进行知识点讲解,是对其他系列博文进行的补充说明,来自于博客园AskScuti. 主题:关于数据库启动流程的三个阶段 内容预览:本篇涉及数据库启动的三个阶段分 ...
- Mac 安装IDEA 2018.3 版本
注:本文转自https://blog.csdn.net/qq_41735004/article/details/86670039 写文文的目的是,怕博主删掉然后找不到所以就写一份 1.下载idea和破 ...
- Tensorflow中one_hot() 函数用法
官网默认定义如下: one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None) 该 ...
- 洛谷P1068 分数线划定
https://www.luogu.org/problem/P1068 #include<bits/stdc++.h> using namespace std; struct Can { ...
- 虫师自动化测试robot Framework 框架的学习
1.python关键字的定义 #coding=utf-8 def add(a,b): return a+b if __name__ == "__main__": c = add(4 ...
- Learning to See in the Dark论文阅读笔记
这是一篇图像增强的论文,作者创建了一个数据集合,和以往的问题不同,作者的创建的see in the dark(SID)数据集合是在极其暗的光照下拍摄的,这个点可以作为一个很大的contribution ...
- D0 设计模式
单一职责 一个类只负责一个功能领域中的相应职责.,就一个类而言,应该只有一个引起它变化的原因. 单一职责原则告诉我们: 一个类不能太"累"! 在软件系统中, 一个类( 大到模块, ...
- PP: Reconstructing time series into a complex network to assess the evolution dynamics of the correlations among energy prices
Purpose detect the dynamics in time series of their correlation Methodology 1. calculate correlation ...