使用代码绑定 DataGridView 控件用于程序界面显示表格
需求
软件界面需要使用表格,对数据进行显示、交互,这是一个非常通用的需求。
实现方法
DataGridView介绍
参考 https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/basic-formatting-and-styling-in-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8
微软的文档介绍的非常清楚,这个winform的控件提供了客制化的图表显示功能。
The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor. For more information, see Basic Formatting and Styling in the Windows Forms DataGridView Control.
You can use a DataGridView control to display data with or without an underlying data source. Without specifying a data source, you can create columns and rows that contain data and add them directly to the DataGridView using the Rows and Columns properties. You can also use the Rows collection to access DataGridViewRow objects and the DataGridViewRow.Cells property to read or write cell values directly. The Item[] indexer also provides direct access to cells.
As an alternative to populating the control manually, you can set the DataSource and DataMember properties to bind the DataGridView to a data source and automatically populate it with data. For more information, see Displaying Data in the Windows Forms DataGridView Control.
When working with very large amounts of data, you can set the VirtualMode property to true to display a subset of the available data. Virtual mode requires the implementation of a data cache from which the DataGridView control is populated. For more information, see Data Display Modes in the Windows Forms DataGridView Control.
For additional information about the features available in the DataGridView control, see DataGridView Control. The following table provides direct links to common tasks.
简单的使用
对于不算复杂的应用,我们只需要为DataGridView指定数据源即可,我们的典型用法是,创建一个list,然后将该list作为控件的数据源。

声明数据源,直接用List的方式,简化设计
///
/// AI EEPROM Information Class,用于声明一个数据行
///
public class AIEEPROMInformation
{
private bool calibration;
private double aigainValue;
private double aioffsetValue;
private double range;
private AICoupling coupling;
private AIImpedance impedance;
private int channel;
public int Channel
{
get
{
return channel;
}
set
{
channel = value;
}
}
public double Range
{
get
{
return range;
}
set
{
range = value;
}
}
public AICoupling Coupling
{
get
{
return coupling;
}
set
{
coupling = value;
}
}
public AIImpedance Impedance
{
get
{
return impedance;
}
set
{
impedance = value;
}
}
public bool Calibration
{
get
{
return calibration;
}
set
{
calibration = value;
}
}
public double AigainValue
{
get
{
return aigainValue;
}
set
{
aigainValue = value;
}
}
public double AioffsetValue
{
get
{
return aioffsetValue;
}
set
{
aioffsetValue = value;
}
}
}
用于声明一个基于AIEEPROMInformation行的数据源
BindingList aiEEPROMList;
aiEEPROMList = new BindingList<AIEEPROMInformation>();
绑定数据源
dataGridView_AIRead.DataSource = aiEEPROMList1;//获取数据源
更新数据源即可完自动成显示
dataGridView_AIRead.Update();
使用代码绑定 DataGridView 控件用于程序界面显示表格的更多相关文章
- ASP通过代码绑定Gridview控件
using System.Configuration;using System.Data.OleDb;using System.Data; public partial class datafilm ...
- Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010
摘 要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...
- C#实现WinForm下DataGridView控件的拷贝和粘贴
DataGridView控件应该是数据库应用系统最常用的控件之一,其方便性不言而喻的.往往用户在使用过程中会提出"从DataGridView空间 中拷贝数据或是向某个DataGridView ...
- DataGridView控件-学习笔记总结
1.GridColor属性用来获取或设置网格线的颜色 dataGridView1.GridColor=Color.Blue; 2.设置宽度 .高度 dataGridView1.Columns[].Wi ...
- 最佳实践扩展Windows窗体DataGridView控件 .net 4.5 附示例代码
Windows窗体DataGridView控件的性能调优.net 4.5 在处理大量数据时, DataGridView 控制可以消耗大量的内存开销,除非你仔细地使用它. 在客户有限的内存,你可以避 ...
- [置顶] DataGridView控件---绑定数据方法
DataGridView控件是在windows应用程中显示数据最好的方式,它只需要几行简短的代码就可以把数据显示给用户,同时又支持增.删.改操作.今天将自己总结的增加数据的方法总结分 ...
- DataGridView:DataGridView控件清空绑定的数据
使用DataGridView控件绑定数据后有时需要清空绑定的数据,在清除DataGridView绑定的数据时: 1.设置DataSource为null this.dgvDemo.DataSource ...
- DataGridView控件绑定数据源
前言: 近期听说DataGridView控件能直接绑定数据源.而不用穿越这层那层的忍辱负重.获取数据.真是高兴的屁颠屁颠的.后来一想二狗肯定不会弄.特意写了一个笨蛋版的教程--也算记录生活.欢度端午了 ...
- DataGridVIew控件绑定数据之后的,增、插、删操作
最开始没有绑定数据,很快就实现了增.插.删操作,可是绑定数据之后,进行这些操作就会报错. 网上对这方面的资料比较少,自己摸索着找到了解决方法,也就是直接对绑定的数据进行操作,这里以DataTable为 ...
随机推荐
- Selenium_使用execute_script执行JavaScript(11)
selenium的包含的方法已能完全满足UI自动化,但是有些时候又不得不用到执行JS的情况,比如在一个富文本框中输入1W个字,使用send_keys方法将经历漫长的输入过程,如果换成使用JS的inne ...
- alias 中使用 awk
alias hehistory10='history |awk "{print \$2}"|sort|uniq -c|sort -rn|head -10' alias lv='ls ...
- 官方文档粗读 - Tutorial
参考: https://www.jianshu.com/p/0d234e14b5d3 1.Connecting 我们通过 create_engine() 来链接数据库,假设我们我们采用SQLite. ...
- 2.OSI各层概述
应用层 1.用户与网络的界面,所有能和用户交互产生网络流量的程序 典型应用层服务:FTP.SMTP.HTTP 表示层 1.用于处理在两个通信系统中交换信息的标识方式 功能1:数据格式变换(翻译) 功能 ...
- 聊聊dubbo协议
搜索关注微信公众号"捉虫大师",后端技术分享,架构设计.性能优化.源码阅读.问题排查.踩坑实践. 协议 协议通俗易懂地解释就是通信双方需要遵循的约定. 我们了解的常见的网络传输协议 ...
- 《剑指offer》面试题27. 二叉树的镜像
问题描述 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入: 4 / \ 2 7 / \ / \ 1 3 6 9 镜像输出: 4 ...
- python极简教程08:对象的方法
测试奇谭,BUG不见. 讲解之前,我先说说我的教程和网上其他教程的区别: 1 我分享的是我在工作中高频使用的场景,是精华内容: 2 我分享的是学习方法,亦或说,是指明你该学哪些.该重点掌握哪些内容: ...
- 【linux】Ubuntu20.04使用apt安装tomcat9
Ubuntu20.04使用apt安装tomcat9 前言 系统环境:ubuntu20.04 java版本:openjdk version "11.0.11" 2021-04-20 ...
- Java构造器(构造方法)
类中的构造器也成为构造方法,是在进行创建对象的时候必须调用的,并且构造器有以下两个特点: 1.必须和类名字相同 2.必须没有返回类型也不能写void public class Demo06 { //一 ...
- Java打印空心三角
Java打印空心三角 public static void main(String[] args) { int n=5; //n表示输出空心三角形行数,这里以5行为例 for(int i=1;i< ...