需求
软件界面需要使用表格,对数据进行显示、交互,这是一个非常通用的需求。
实现方法
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();
绑定数据源
dataGridView_AIRead.DataSource = aiEEPROMList1;//获取数据源
更新数据源即可完自动成显示
dataGridView_AIRead.Update();