创建DataTable与DataGridView进行绑定
private DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
/// <summary>
/// 初始化DataTable
/// </summary>
public void InitDataTable()
{
//不允许自动生成,若改为允许,界面会自动增加DataTable列,那么界面上既会包含DataGridView中定义的列,也会包含DataTable定义的列
this.dataGridView1.AutoGenerateColumns = false;
DataColumn col = new DataColumn("No", typeof(int));
dt.Columns.Add(col);
dt.Columns.Add(new DataColumn("Addr", typeof(string)));
dt.Columns.Add(new DataColumn("FuntionType", typeof(string)));
dt.Columns.Add(new DataColumn("Result", typeof(string)));
bs.DataSource = dt;
this.dataGridView1.DataSource = bs;
//将DataGridView中的列与DataTable中的列进行数据绑定,this.cloNum为列名
this.colNum.DataPropertyName = "No";
this.colAddress.DataPropertyName = "Addr";
this.colFunction.DataPropertyName = "FuntionType";
this.colResult.DataPropertyName = "Result";
}
2.也可以只创建一个DataGridView,而不创建任何的列,直接用BingdingSource绑定的DataTable来绑定DataGridView的数据源,那么DataTable中的所有数据便会显示在界面上,有一个缺点,就是界面上显示的列名称是DataTable中的名称,而不能自定义了。好处是不需要再操作复杂的DataGridView来对页面进行数据的增删改,而只要操作DataTable即可。
创建DataTable与DataGridView进行绑定的更多相关文章
- Datagridview 列绑定
Datagridview 列绑定 dataGridView1.Columns.Clear(); dataGridView1.Columns.Add("id", "id&q ...
- C#创建datatable (转)
C#创建datatable 方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc ...
- C#: 线程间操作无效: 从不是创建控件“dataGridView”的线程访问它
最近在修改自动化小工具,用多线程来解决后台拷贝导致WinForm界面卡死的情况,但是遇到过错:线程间操作无效: 从不是创建控件“dataGridView”的线程访问它. 这是因为在多线程程序中,新创建 ...
- C# datagridview列绑定类中类的属性
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://www.cnblogs.com/linghaoxinpian/p/5906374. ...
- <转>C# 动态创建DataTable
C# 动态创建DataTable,有时候在做些测试Demo中用来模拟一些数据比较不错.记在这里避免以后重写呵呵... DataTable dt = new DataTable(); dt.Column ...
- 手动创建DataTable并绑定gridview
原文发布时间为:2008-08-04 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- DataGridView如何绑定DataRow对象集合
DataGridView对象是我们在进行Winform程序开发中经常使用的呈现数据的控件,而数据则是通过DataSource这个Property来设置的.根据MSDN的说明,DataGridView对 ...
- C#创建datatable
Asp.net DataTable添加列和行的方法 方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc ...
- 动态创建DataTable总结
最简单的: DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("name" ...
随机推荐
- bootstrap环境
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- C#常用的form窗体属性(最大化、最小化、窗体居中)
一.窗体居中 //窗体居中 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 二.去掉最小化.最大化 ...
- 箭头函数 this指向问题
1.为什么要用箭头函数 简洁 易用 固定this 指向(箭头函数在this定义时候生效) 2.箭头函数分析this指向 1.this指向调用函数的对象 情况1 var obj={ a:"1& ...
- Go_Json序列化
1. json介绍 2. json格式说明 3. json序列化 3.1 结构体序列化 package main import ( "fmt" "encoding/jso ...
- Python单例
01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的成熟的解决方案 使用 设计模式 是为了可重用代码.让代码更容易被他人理解.保 ...
- WiFi密码破解(wpa/wpa2)
参考一篇很好的贴子:https://www.cnblogs.com/daoyi/p/Kali-Linux-shi-yongAircrack-po-jiewifi-mi-ma-wpawp.html #前 ...
- 前端之HTML基础篇
HTML基础篇 目录 本章内容: 简介 1. ...
- yum grouplist 安装gnome桌面环境
经常,我们如果需要安装一些比较复杂的软件时,都会在安装操作系统的时候直接勾选,然后进行安装.但是,有的时候,等操作系统安装完了才发现有遗漏的软件没有安装. 这个时候,yum就要出来救场了.使用yu ...
- unittest 改装框架ascii 排序执行用例,按照自己书写先后顺序执行
设计思路: 获取成员变量class.__dict__.keys() filter过滤符合要求成员,由于3.x成员dict属性是支持有序的 # coding=utf-8import unittestfr ...
- [C++]蛇形填数
[从左下角开始,逆时针蛇形填数] #include <iostream> using namespace std; int main() { int n; cin>>n; in ...